【问题标题】:boost move compile error提升移动编译错误
【发布时间】:2017-05-16 04:22:49
【问题描述】:

我正在尝试在类主体之外实现移动构造函数,但它不会正确编译

#include <boost/move/move.hpp>

class Test
{
    BOOST_COPYABLE_AND_MOVABLE(Test)
public:
    Test() {}
    Test(const Test & other) { }
    Test(BOOST_RV_REF(Test) other);
    Test & operator=(BOOST_COPY_ASSIGN_REF(Test) other) { return *this; } 
    Test & operator=(BOOST_RV_REF(Test) other) { return *this; }
};

Test::Test(BOOST_RV_REF(Test) other) { }

我用g++编译了这段代码,我的g++版本是4.4.7

$ g++ -c test.cpp
test.cpp:15: error: prototype for 'Test::Test(boost::rv<Test>&)' does not match any in class 'Test'
test.cpp:9: error: candidates are: Test::Test(boost:rv<Test>&)
test.cpp:8: error:                 Test::Test(const Test&)
test.cpp:7: error:                 Test::Test()

【问题讨论】:

  • 似乎在 MSVC 2013 中编译得很好
  • g++ 5.4.0 也失败了

标签: c++ boost move


【解决方案1】:

g++ 5.4.0 也失败了 – flyzero

必须是您的增强版本。

它适用于 g++ 5.4.1 和 Boost 1.64。如果没有,请检查预处理器输出是否有任何包含/宏故障。

【讨论】:

    【解决方案2】:

    在 Linux 中,::boost::rv 是用 may_alias 属性声明的。删除 may_alias 属性后,我的代码编译正确。

    #define BOOST_MOVE_ATTRIBUTE_MAY_ALIAS __attribute__((__may_alias__))
    
    template <class T>
    class rv
        : public ::boost::move_detail::if_c
            < ::boost::move_detail::is_class<T>::value
            , T
            , ::boost::move_detail::nat
            >::type
    {
        rv();
        ~rv() throw();
        rv(rv const&);
        void operator=(rv const&);
    } BOOST_MOVE_ATTRIBUTE_MAY_ALIAS;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 2014-03-07
      • 2016-12-31
      • 2010-10-11
      相关资源
      最近更新 更多