2015-04-23 70 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中得到了修復 –

相關問題