混合 std::move() 和 std::thread 无法编译

Mixing std::move() and std::thread won't compile

本文关键字:std 编译 thread move 混合      更新时间:2023-10-16

具有如下代码:

#include <memory>
#include <thread>
class A
{
  void foo(int&& arg) const {}
  void boo() const 
  {
    int value(0);
    std::thread t(&A::foo, this, std::move(value));
    t.join();
  }
};
int main()
{
  A a;
  return 0;
}

MS Visual Studio 2012 (工具集 v110) 给出下一个错误:

错误 C2664:"_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,>::运算符 ()(常量 _Wrapper &,_V0_t) const' : 无法转换参数 2 从 'int' 到 'int &&&'

什么?我们不能通过线程使用移动语义吗?

这是

VS中的错误。你可以看到这个:

https://connect.microsoft.com/VisualStudio/feedback/details/737812

//打开时间: 4/19/2012 20:58:36, 嗯:)

以及他们页面的解决方法:

您可以使用std::ref,但它不一样。

已关闭为固定,因此您可能永远需要使用工具或使用"解决方法"。