Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 知道为什么std::move在这里失败了吗? #包括 #包括 #包括 #包括 #包括 #包括 无效ThFun(标准::承诺和prms) { std::string hello=“来自未来的你好!\n”; prms.set_值(hello); } int main() { std::promise-prms; auto-ftr=prms.get_future(); std::thread th(&ThFun,std::move(prms)); std::cout with 1> [ 1> _Ty=std::string 1> ] 1> 不能将左值绑定到右值引用 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\functional(1140):编译类模板成员函数“void std::_Bind::operator()(void)”时 1> 与 1> [ 1> _Forced=true, 1> _Ret=无效, 1> _Fun=void(uu cdecl*const)(标准::承诺和&), 1> _V0_t=标准::承诺, 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> =std::Nil 1> ] 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\thread(50):请参阅正在编译的类模板实例化“std::\u Bind”的参考 1> 与 1> [ 1> _Forced=true, 1> _Ret=无效, 1> _Fun=void(uu cdecl*const)(标准::承诺和&), 1> _V0_t=标准::承诺, 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> =std::Nil 1> ] 1> c:\users\jim\documents\visualstudio 11\projects\promisesandfutures\promisesandfutures\main.cpp(18):请参阅正在编译的函数模板实例化“std::thread::thread(_Fn,_V0_t&)” 1> 与 1> [ 1> _Ty=std::string, 1> _Fn=无效(uuu cdecl*)(标准::承诺和&), 1> _V0_t=标准::承诺 1> ]_C++_Visual C++_C++11 - Fatal编程技术网

C++ 知道为什么std::move在这里失败了吗? #包括 #包括 #包括 #包括 #包括 #包括 无效ThFun(标准::承诺和prms) { std::string hello=“来自未来的你好!\n”; prms.set_值(hello); } int main() { std::promise-prms; auto-ftr=prms.get_future(); std::thread th(&ThFun,std::move(prms)); std::cout with 1> [ 1> _Ty=std::string 1> ] 1> 不能将左值绑定到右值引用 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\functional(1140):编译类模板成员函数“void std::_Bind::operator()(void)”时 1> 与 1> [ 1> _Forced=true, 1> _Ret=无效, 1> _Fun=void(uu cdecl*const)(标准::承诺和&), 1> _V0_t=标准::承诺, 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> =std::Nil 1> ] 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\thread(50):请参阅正在编译的类模板实例化“std::\u Bind”的参考 1> 与 1> [ 1> _Forced=true, 1> _Ret=无效, 1> _Fun=void(uu cdecl*const)(标准::承诺和&), 1> _V0_t=标准::承诺, 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> =std::Nil 1> ] 1> c:\users\jim\documents\visualstudio 11\projects\promisesandfutures\promisesandfutures\main.cpp(18):请参阅正在编译的函数模板实例化“std::thread::thread(_Fn,_V0_t&)” 1> 与 1> [ 1> _Ty=std::string, 1> _Fn=无效(uuu cdecl*)(标准::承诺和&), 1> _V0_t=标准::承诺 1> ]

C++ 知道为什么std::move在这里失败了吗? #包括 #包括 #包括 #包括 #包括 #包括 无效ThFun(标准::承诺和prms) { std::string hello=“来自未来的你好!\n”; prms.set_值(hello); } int main() { std::promise-prms; auto-ftr=prms.get_future(); std::thread th(&ThFun,std::move(prms)); std::cout with 1> [ 1> _Ty=std::string 1> ] 1> 不能将左值绑定到右值引用 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\functional(1140):编译类模板成员函数“void std::_Bind::operator()(void)”时 1> 与 1> [ 1> _Forced=true, 1> _Ret=无效, 1> _Fun=void(uu cdecl*const)(标准::承诺和&), 1> _V0_t=标准::承诺, 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> =std::Nil 1> ] 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\thread(50):请参阅正在编译的类模板实例化“std::\u Bind”的参考 1> 与 1> [ 1> _Forced=true, 1> _Ret=无效, 1> _Fun=void(uu cdecl*const)(标准::承诺和&), 1> _V0_t=标准::承诺, 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> =std::Nil 1> ] 1> c:\users\jim\documents\visualstudio 11\projects\promisesandfutures\promisesandfutures\main.cpp(18):请参阅正在编译的函数模板实例化“std::thread::thread(_Fn,_V0_t&)” 1> 与 1> [ 1> _Ty=std::string, 1> _Fn=无效(uuu cdecl*)(标准::承诺和&), 1> _V0_t=标准::承诺 1> ],c++,visual-c++,c++11,C++,Visual C++,C++11,我使用的是VC11。错误是无法绑定左值,但我使用的是std::move使其成为右值 谢谢 编辑 std::ref工作正常。从错误消息看,VC11似乎犯了gcc在gcc 4.7之前犯的相同错误:在std::thread的内部使用std::bind要求参数可复制,但std::thread不可复制。这意味着只移动等类型e> std::promise不能作为参数传递给线程 事实上,您的示例适用于gcc 4.7或MSVC 2010和my,但在使用gcc 4.6的本机库时出现类似的错误消息(gcc 4.6在

我使用的是VC11。错误是无法绑定左值,但我使用的是std::move使其成为右值

谢谢

编辑
std::ref工作正常。

从错误消息看,VC11似乎犯了gcc在gcc 4.7之前犯的相同错误:在
std::thread
的内部使用
std::bind
要求参数可复制,但
std::thread
不可复制。这意味着只移动等类型e> std::promise不能作为参数传递给线程


事实上,您的示例适用于gcc 4.7或MSVC 2010和my,但在使用gcc 4.6的本机库时出现类似的错误消息(gcc 4.6在使用just::thread时也适用)。

我将在msft上作为错误发布。谢谢。
#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>          ]