2015-09-05 100 views
2

我的環境是Visual Stuido 2013,VC12,Boost 1.59。 下面的代碼(真正的代碼,最小的攝製):Boost可選Boost線程編譯問題

#include "boost/thread.hpp" 
#include "boost/optional.hpp" 

class MyClass 
{ 
public: 

    template <typename T> 
    operator const T&() const; 

}; 

boost::optional<MyClass> foo() 
{ 
    boost::optional<MyClass> res; 
    return res; 
} 

int main(int argc) 
{ 
    foo(); 
} 

無法編譯,錯誤:

 
    1>------ Build started: Project: TestBoostOptional, Configuration: Debug x64 ------ 
    1> main.cpp 
    1>c:\workspace\third_party\boost_1_59_0\boost/optional/optional.hpp(297): error C2664: 'void boost::optional_detail::optional_base::construct(MyClass &&)' : cannot convert argument 1 from 'boost::detail::thread_move_t' to 'const MyClass &' 
    1>   with 
    1>   [ 
    1>    T=MyClass 
    1>   ] 
    1>   Reason: cannot convert from 'boost::detail::thread_move_t' to 'const MyClass' 
    1>   with 
    1>   [ 
    1>    T=MyClass 
    1>   ] 
    1>   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 
    1>   c:\workspace\third_party\boost_1_59_0\boost/optional/optional.hpp(292) : while compiling class template member function 'boost::optional_detail::optional_base::optional_base(boost::optional_detail::optional_base &&)' 
    1>   with 
    1>   [ 
    1>    T=MyClass 
    1>   ] 
    1>   c:\workspace\third_party\boost_1_59_0\boost/optional/optional.hpp(873) : see reference to function template instantiation 'boost::optional_detail::optional_base::optional_base(boost::optional_detail::optional_base &&)' being compiled 
    1>   with 
    1>   [ 
    1>    T=MyClass 
    1>   ] 
    1>   c:\workspace\third_party\boost_1_59_0\boost/optional/optional.hpp(766) : see reference to class template instantiation 'boost::optional_detail::optional_base' being compiled 
    1>   with 
    1>   [ 
    1>    T=MyClass 
    1>   ] 
    1>   main.cpp(14) : see reference to class template instantiation 'boost::optional' being compiled 

注意#include "boost/thread.hpp"。刪除代碼時包括代碼編譯。任何可以解決的辦法?

回答

3

在使用任何boost頭之前,您必須定義BOOST_THREAD_USES_MOVE

#define BOOST_THREAD_USES_MOVE 

更多信息位於here。這個定義模仿了Boost.Move這個在這裏需要的移動。

In order to implement Movable classes, move parameters and return types Boost.Thread uses the rvalue reference when the compiler support it. On compilers not supporting it Boost.Thread uses either the emulation provided by Boost.Move or the emulation provided by the previous versions of Boost.Thread depending whether BOOST_THREAD_USES_MOVE is defined or not. This macros is unset by default when BOOST_THREAD_VERSION is 2. Since BOOST_THREAD_VERSION 3, BOOST_THREAD_USES_MOVE is defined.

另見Boost.Move

Boost.Thread uses by default an internal move semantic implementation. Since version 3.0.0 you can use the move emulation emulation provided by Boost.Move.

When BOOST_THREAD_VERSION==2 define BOOST_THREAD_USES_MOVE if you want to use Boost.Move interface. When BOOST_THREAD_VERSION==3 define BOOST_THREAD_DONT_USE_MOVE if you don't want to use Boost.Move interface.

+0

令人印象深刻的回答。我不知道的東西。並有有用的文檔報價和鏈接。我們還可以要求什麼:) – sehe

+0

接受這個,另一個選擇是'#define BOOST_THREAD_VERSION 3'。然而這並不能解釋爲什麼會發生這種情況 –