Why is this not compiling? (RValue as thread CTOR

2019-01-29 00:01发布

问题:

Hello here is a test code I wrote on MSVC12.

Could someone tell me why the std::move when I pass parameters to the thread are not converting the variabes to RValue refs?? And what I should do.

Thank you!

///some arbitrary long task
std::string DumpFile(std::string path){
  std::this_thread::sleep_for(std::chrono::seconds(10));
  return path;
}


void run_promise(std::promise<std::string> &&_prom, std::string &&_path){
  try {
    std::string val = DumpFile(std::move(_path));
    _prom.set_value(val);
  }
  catch (...) {
    _prom.set_exception(std::current_exception());
  }
}

std::future<std::string> ADumpFile(std::string && path) {
  std::promise<std::string> prms;
  std::future<std::string> fut = prms.get_future();
  std::thread th(run_promise, std::move(prms), std::move(path));
  th.detach();
  return fut;
}

int main(int argc, char* argv []){
  auto fut = ADumpFile("toto");
  while (fut.wait_for(std::chrono::seconds(1))!=std::future_status::ready){
    std::cout << "waiting\n";
  }
  auto res = fut.get();
  getchar();
  return 0;
}

The error I get is :

Error 1 error C2664: 'void (std::promise<std::string> &&,std::string &&)' : cannot convert argument 2 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'std::string &&' C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional 1149 1 demo11 Error 2 error C2664: 'void (std::promise<std::string> &&,std::string &&)' : cannot convert argument 1 from 'std::promise<std::string>' to 'std::promise<std::string> &&' C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional 1149 1 demo11

Although, if I call run_promise(std::move(prom),std::move(path)) I have no problem.

Is this an issue related to passing the rvalue arguments through the thread CTOR?

回答1:

Well, this problem was reported for VC11 http://connect.microsoft.com/VisualStudio/feedback/details/737812 and seems MS didn't address in VC12.