2015-04-23 61 views
5

有代码如下:混合的std ::移动()和std ::线程不会​​编译

#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)给出了一个错误:

error C2664: '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,>::operator()(const _Wrapper &,_V0_t) const' : cannot convert parameter 2 from 'int' to 'int &&'

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

+3

对于它的价值,它可以在g ++和clang中进行编译。 – user2079303

回答

9

这是VS中的错误。你可以看到这一点:

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

// 开业时间:4/19/2012下午8点58分36秒,嗯:)从他们的页面

而且解决方法:

You can use std::ref , but it's not the same.

以固定方式关闭,因此您可能需要使用永不工具或使用“解决方法”。

+0

谢谢!我花了三年时间才遇到这样的事情 –

+0

[std :: thread构造函数不处理移动对象](https://connect.microsoft.com/VisualStudio/feedback/details/729886/std-thread-constructor-doesnt -handle-movable-object)表示它在VS 2015 RTM中得到了修复 –