Tav*_*son 2 c++ visual-c++ c++11
#include <thread>
#include <cassert>
#include <iostream>
#include <string>
#include <future>
#include <utility>
void ThFun(std::promise<std::string>&& prms)
{
std::string hello = "Hello From Future!\n";
prms.set_value(hello);
}
int main()
{
std::promise<std::string> prms;
auto ftr = prms.get_future();
std::thread th(&ThFun, std::move(prms));
std::cout << "Hello from Main\n";
std::string str = ftr.get();
std::cout << str << std::endl;
th.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到这个构建错误.
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1140): error C2664: 'void (std::promise<_Ty> &&)' : cannot convert parameter 1 from 'std::promise<_Ty>' to 'std::promise<_Ty> &&'
1> with
1> [
1> _Ty=std::string
1> ]
1> You cannot bind an lvalue to an rvalue reference
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1140) : while compiling class template member function 'void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()(void)'
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=void (__cdecl *const )(std::promise<std::string> &&),
1> _V0_t=std::promise<std::string>,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(50) : see reference to class template instantiation 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>' being compiled
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=void (__cdecl *const )(std::promise<std::string> &&),
1> _V0_t=std::promise<std::string>,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> c:\users\jim\documents\visual studio 11\projects\promisesandfutures \promisesandfutures\main.cpp(18) : see reference to function template instantiation 'std::thread::thread<void(__cdecl *)(std::promise<_Ty> &&),std::promise<_Ty>>(_Fn,_V0_t &&)' being compiled
1> with
1> [
1> _Ty=std::string,
1> _Fn=void (__cdecl *)(std::promise<std::string> &&),
1> _V0_t=std::promise<std::string>
1> ]
Run Code Online (Sandbox Code Playgroud)
我正在使用VC11.错误说不能绑定左值,但我使用std :: move使其成为右值.
谢谢.
编辑 std :: ref工作正常.
从错误消息看起来VC11犯了与gcc在gcc 4.7之前所做的相同的错误:std::bind在内部使用std::thread.std::bind要求参数是可复制的,但std::thread不是.这意味着仅移动类型std::promise不能作为参数传递给线程.
实际上,您的示例适用于gcc 4.7,或者使用MSVC 2010和我的C++ 11线程库的just :: thread实现,但是在使用其本机库时,gcc 4.6失败时出现类似的错误消息(gcc 4.6也适用于使用just :: thread).
| 归档时间: |
|
| 查看次数: |
982 次 |
| 最近记录: |