以下示例在使用带有 --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++ test-cont.cpp In include/c++/4.4.6/memory:49, boost/container/container_fwd.hpp:36, boost/container/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
语义,我假设在使用boost::move
c++03 编译器编译时会自动使用这些语义。
如果我手动修改struct data
定义:
- 默认构造函数
- 复制构造函数
- 赋值运算符
- 使用 BOOST_RV_REF 的模拟移动构造函数
- 使用 BOOST_RV_REF 的模拟移动赋值运算符
该示例编译。
必须为 C++03 手动添加这些移动/复制构造函数和赋值运算符很费力。
这是一个错误boost::container
吗?如果是这样,向 boost 社区报告的最佳方式是什么。