c++ - Boost 可选,带有 Boost Thread 编译问题

标签 c++ boost boost-thread boost-optional

我的环境是Visual Stuido 2013,VC12,Boost 1.59。 以下代码(真实代码的最小重现):

#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"。删除此包含时,代码将进行编译。有什么办法可以解决这个问题吗?

最佳答案

在使用任何 boost header 之前,您必须定义 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.

关于c++ - Boost 可选,带有 Boost Thread 编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32417196/

相关文章:

c++ - 内部类和初始化

c++ - 一条定义规则警告

c++ - 如何禁用#pragma 警告?

c++ - CMake error no CMAKE_C_COMPILER can be found using Xcode and GLFW

c++ - boost::function 和 boost::lambda - 调用站点调用并访问 _1 和 _2 作为类型

c++ - 使用额外参数 boost 变体访问者

c++ - Boost:迭代图像区域

c++ - boost::_bi::unwrapper<F>::unwrap 不能作为函数使用?

c++ - 使用自由函数声明 unordered_set 用于用户定义的散列和比较

c++ - 为提升线程命名?