C ++ 11 std :: thread std :: move抱怨尝试使用已删除的函数

时间:2016-12-19 00:40:01

标签: c++ multithreading c++11

我正在学习C ++ 11线程并尝试编写一个更改共享内存的线程。我分别使用了std::refstd::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;
}

1 个答案:

答案 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 &