提高::容器:: vector的失败,C ++ 03编译器编译 [英] boost::container::vector fails to compile with C++03 compiler
问题描述
使用GCC 4.4.6当与--std = C ++ 0x中的国旗,但无法编译在C ++ 03模式下面的例子编译罚款。
的#include< stdint.h>
#包括LT&;升压/集装箱/ vector.hpp>结构数据
{
INT I_;
提高::容器::矢量<&数据GT; V_;
};INT主(INT ARGC,字符** argv的)
{
数据MYDATA的;
myData.i_ = 10; 数据myData2;
myData2.i_ = 30; myData.v_.push_back(myData2); 返回0;
}
这与编译成功
G ++ --std =的C ++ 0x测试cont.cpp
但是,如果我删除 - STD =的C ++ 0x
我收到以下错误:
G ++测试cont.cpp
在文件中包含来自
包括/ C ++ / 4.4.6 /内存:49,
升压/集装箱/ container_fwd.hpp:36,
升压/集装箱/ vector.hpp:20,
从测试cont.cpp:2:
包括/ C ++ / 4.4.6 /位/ stl_algobase.h:在静态成员函数
静态_OI的std :: __ copy_move<假的,假的,性病:: random_access_iterator_tag> ::
__copy_m(_II,_II,_OI)与_II =
提高::容器:: constant_iterator<数据,长整型>中_OI =数据*]:包括/ C ++ / 4.4.6 /位/ stl_algobase.h:397:从实例
_OI的std :: __ copy_move_a(_II,_II,_OI)
[与布尔_IsMove =假,
_II =的boost ::容器:: constant_iterator<数据,长整型>中_OI =数据*]包括/ C ++ / 4.4.6 /位/ stl_algobase.h:436:从实例
_OI的std :: __ copy_move_a2(_II,_II,_OI)
[与布尔_IsMove =假,
_II =的boost ::容器:: constant_iterator<数据,长整型>中_OI =数据*]包括/ C ++ / 4.4.6 /位/ stl_algobase.h:468:从实例
_OI的std ::复制(_II,_II,_OI)
[与_II =的boost ::容器:: constant_iterator<数据,长整型>中_OI =数据*]升压/移动/ move.hpp:1147:从实例
提高:: copy_or_move(I,I,F,
TYPENAME的boost :: move_detail :: disable_if<提高:: move_detail :: is_move_iterator<我>中无效> ::型*)
[其中i =的boost ::容器:: constant_iterator<数据,长整型>中F =数据*]升压/集装箱/细节/ advanced_insert_int.hpp:58:从实例
无效的boost ::容器:: container_detail :: advanced_insert_aux_proxy< A,FwdIt,迭代器> :: copy_remaining_to(迭代器)
[与A =的std ::分配器<数据>中FwdIt =提振::容器:: constant_iterator<数据,长整型>中迭代=数据*]测试cont.cpp:21:从这里实例化包括/ C ++ / 4.4.6 /位/ stl_algobase.h:343:错误:
敌不过运算符=在* __result =
__first.boost ::容器:: constant_iterator< T,差> ::运算符*
[与T =数据,差=长整型]()测试cont.cpp:5:注意:考生:数据&安培;数据::运算符=(数据&安培;)
它看起来像的boost ::容器:: vector的需要,我认为会自动使用移动
语义的boost ::移动
当用C ++编译器03编译。
如果我手动修改结构数据
来定义:
- 默认的构造函数
- 拷贝构造函数
- 赋值运算符
- 使用BOOST_RV_REF一个模拟移动构造函数
- 使用BOOST_RV_REF一个模拟移动赋值操作符
这个例子编译。
不必手动添加这些移动/拷贝构造函数和赋值操作符的C ++ 03费力。
这是与的boost ::容器
中的错误?如果是的话是什么将其报告给升压社会的最好方式。
这是一个限制,以仿真的C ++ 03(<一个href=\"http://www.boost.org/doc/libs/1_51_0/doc/html/move/emulation_limitations.html#move.emulation_limitations.assignment_operator\"相对=nofollow>从这里):
该宏BOOST_COPYABLE_AND_MOVABLE需要定义的副本
构造copyable_and_movable采取在非const的参数
C ++编译器03:
块引用>//由BOOST_COPYABLE_AND_MOVABLE生成
copyable_and_movable&放大器;运算符=(copyable_and_movable&安培;){/ ** /}
由于生成的拷贝构造函数的非const超载,
编译器生成的赋值运算符含类
copyable_and_movable将获得非const拷贝构造函数重载,
这必将带来惊喜的用户。
此限制迫使用户定义副本的const版本
转让,在所有类拷贝的持有和活动类哪些
恼人的可能在某些情况下
块引用>在你的情况
的boost ::容器::矢量&lt;&数据GT; v _;
使用BOOST_COPYABLE_AND_MOVABLE(矢量)
因此误差The following example compiles fine when using GCC 4.4.6 with the --std=c++0x flag but fails to compile in C++03 mode.
#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; }
It compiles successfully with
g++ --std=c++0x test-cont.cpp
However if I remove the
--std=c++0x
I get the following errors: g++ test-cont.cpp In file included from include/c++/4.4.6/memory:49, boost/container/container_fwd.hpp:36, boost/container/vector.hpp:20, from 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&)
It looks like boost::container::vector requires
move
semantics which I assumed would automatically useboost::move
when compiled with a c++03 compiler.If I manually modify the
struct data
to define:
- a default constructor
- a copy constructor
- an assignment operator
- an emulated move constructor using BOOST_RV_REF
- an emulated move assignment operator using BOOST_RV_REF
The example compiles.
Having to manually add these move/copy constructors and assignment operators for C++03 is laborious.
Is this a bug with
boost::container
? If so what is the best way to report it to the boost community.解决方案This is a limitation to the emulation in 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.
In your case
boost::container::vector<data> v_;
usesBOOST_COPYABLE_AND_MOVABLE(vector)
hence the error.这篇关于提高::容器:: vector的失败,C ++ 03编译器编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!