c++ - boost::container::vector 无法使用 C++03 编译器进行编译

标签 c++ boost c++11

以下示例在使用带有 --std=c++0x 标志的 GCC 4.4.6 时编译正常,但无法在 C++03 模式下编译。

#include <stdint.h>
#include <boost/container/vector.hpp>

struct data
{
   int               i_;
   boost::container::vector<data>      v_;
};

int main( int argc, char** argv )
{
    data myData;
    myData.i_ = 10;

    data myData2;
    myData2.i_ = 30;

    myData.v_.push_back( myData2 );

    return 0;
}

编译成功

 g++ --std=c++0x test-cont.cpp

但是,如果我删除 --std=c++0x,我会收到以下错误: g++ 测试cont.cpp 在包含的文件中 包括/c++/4.4.6/内存:49, boost/容器/container_fwd.hpp:36, boost/容器/vector.hpp:20, 来自 test-cont.cpp:2:

include/c++/4.4.6/bits/stl_algobase.h: In static member function 
    static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::
        __copy_m(_II, _II, _OI) [with _II = 
            boost::container::constant_iterator<data, long int>, _OI = data*]:

include/c++/4.4.6/bits/stl_algobase.h:397:   instantiated from 
    _OI std::__copy_move_a(_II, _II, _OI) 
        [with bool _IsMove = false, 
                   _II = boost::container::constant_iterator<data, long int>, _OI = data*]

include/c++/4.4.6/bits/stl_algobase.h:436:   instantiated from 
    _OI std::__copy_move_a2(_II, _II, _OI) 
    [with bool _IsMove = false, 
               _II = boost::container::constant_iterator<data, long int>, _OI = data*]

include/c++/4.4.6/bits/stl_algobase.h:468:   instantiated from 
    _OI std::copy(_II, _II, _OI) 
        [with _II = boost::container::constant_iterator<data, long int>, _OI = data*]

boost/move/move.hpp:1147:   instantiated from 
    boost::copy_or_move(I, I, F, 
        typename boost::move_detail::disable_if< boost::move_detail::is_move_iterator<I>, void>::type*) 
        [with I = boost::container::constant_iterator<data, long int>, F = data*]

boost/container/detail/advanced_insert_int.hpp:58:   instantiated from 
    void boost::container::container_detail::advanced_insert_aux_proxy<A, FwdIt, Iterator>::copy_remaining_to(Iterator) 
    [with A = std::allocator<data>, FwdIt = boost::container::constant_iterator<data, long int>, Iterator = data*]

test-cont.cpp:21:   instantiated from here

include/c++/4.4.6/bits/stl_algobase.h:343: error: 
    no match for operator= in * __result = 
        __first.boost::container::constant_iterator<T, Difference>::operator* 
        [with T = data, Difference = long int]()

test-cont.cpp:5: note: candidates are: data& data::operator=(data&)

看起来 boost::container::vector 需要 move 语义,我认为在使用 c++03 编译器编译时会自动使用 boost::move

如果我手动修改struct data来定义:

  1. 默认构造函数
  2. 复制构造函数
  3. 赋值运算符
  4. 使用 BOOST_RV_REF 的模拟移动构造函数
  5. 使用 BOOST_RV_REF 的模拟移动赋值运算符

示例编译。

必须为 C++03 手动添加这些移动/复制构造函数和赋值运算符非常费力。

这是 boost::container 的错误吗?如果是这样,向 boost 社区报告它的最佳方式是什么。

最佳答案

这是 C++03 中仿真的限制 (from here):

The macro BOOST_COPYABLE_AND_MOVABLE needs to define a copy constructor for copyable_and_movable taking a non-const parameter in C++03 compilers:

//Generated by BOOST_COPYABLE_AND_MOVABLE
copyable_and_movable &operator=(copyable_and_movable&){/**/}

Since the non-const overload of the copy constructor is generated, compiler-generated assignment operators for classes containing copyable_and_movable will get the non-const copy constructor overload, which will surely surprise users. This limitation forces the user to define a const version of the copy assignment, in all classes holding copyable and movable classes which might annoying in some cases.

在你的情况下 boost::container::vector<data> v_;使用 BOOST_COPYABLE_AND_MOVABLE(vector)因此错误。

关于c++ - boost::container::vector 无法使用 C++03 编译器进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13083360/

相关文章:

c++ - 为什么这在 C 中编译而不是 C++ (sigaction)?

c++ - Vulkan 中多个具有透明度的绘制调用之间是否需要同步?

c++ 这个指针不执行构造函数

c++ - Boost mpi挂起

c++ - 构造函数在哪里获取/设置默认分配器?

java - 在 C++、Java、C# 之间循环初始化变量作用域

c++ - 如何扩展分配的命名空间

c++ - 将 vector 作为指针传递

c++ - 带有 boost::fast_pool_allocator 的模板模板参数

c++ - static_assert 和类模板