#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;
}
我得到这个构建错误。
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> ]
我正在使用VC11。错误说不能绑定左值,但我正在使用 std::move 使其成为右值。
谢谢。
编辑 std::ref 工作正常。