我正在学习C ++ 11线程并尝试编写一个更改共享内存的线程。我分别使用了std::ref
和std::move
。我使用:g++ eg3.cpp -std=c++11 -pthread
运行代码。但是我发现std::move
在我的Mac上不起作用。我得到这样的错误:
In file included from eg3.cpp:1: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:337:5: error:
attempt to use a deleted function
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
^
...
我的代码如下:
#include<thread>
#include<iostream>
#include<mutex>
#include<condition_variable>
#include<string>
#include<functional>
#include<utility>
using namespace std;
int main(){
string s = "Hello!";
cout << "Main before: " << s << endl;
// thread t([](string& s){cout << s << endl; s = "Ni hao!";}, ref(s)); //// This works!
// thread t([](string& s){cout << s << endl; s = "Ni hao!";}, move(s)); //// This does not work
t.join();
cout << "Main after: " << s << endl;
return 0;
}
答案 0 :(得分:2)
你只需要使lambda取$stmt = $db->prepare("INSERT INTO Table_Name (SensitiveData) VALUES (AES_ENCRYPT(?,UNHEX(SHA2('RandomStringofData',512))))");
$stmt->bind_param('s',$SensitiveData);
$stmt->execute();
$stmt->close();
(按值)或string
(通过常量引用)或string const &
(rvalue reference)来支持移动。在这种情况下,由于您正在修改string &&
,因此无法使用s
。
string const &
失败是因为你无法将rvalue引用(thread t([](string && s){cout << s << endl; s = "Ni hao!";}, move(s));
)传递给带有左值引用(string &&
)的函数/ lambda。
一个简化的例子是
string &