为什么不编译? (RValue作为线程CTOR参数) [英] Why is this not compiling? (RValue as thread CTOR arguments)

查看:101
本文介绍了为什么不编译? (RValue作为线程CTOR参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是我在MSVC12上编写的测试代码.

Hello here is a test code I wrote on MSVC12.

有人可以告诉我为什么当我将参数传递给线程时std :: move不能将变量转换为RValue引用吗? 我该怎么办.

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.

谢谢!

///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;
}

我得到的错误是:

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

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

尽管,如果我打电话给 run_promise(std::move(prom),std::move(path)),我没问题.

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

这与通过线程CTOR传递右值参数有关吗?

推荐答案

好,对于VC11报告了此问题 http://connect.microsoft.com/VisualStudio/feedback/details/737812 ,似乎MS在VC12中没有解决.

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

这篇关于为什么不编译? (RValue作为线程CTOR参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆