2012-10-26 103 views
3

以下示例在使用GCC 4.4.6時使用--std = C++ 0x標誌進行編譯,但無法在C++ 03中進行編譯模式。boost :: container :: vector無法使用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, 升壓/容器/ container_fwd.hpp:36, 升壓/容器/ vector.hpp:20, 從測試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定義:

  1. 默認的構造函數
  2. 拷貝構造函數
  3. 賦值運算符
  4. 使用BOOST_RV_REF
  5. 一個模擬移動分配的模擬移動構造函數運營商使用BOOST_RV_REF

該示例編譯。

必須爲C++ 03手動添加這些移動/複製構造函數和賦值運算符是很費力的。

這是一個缺陷boost::container?如果是的話,向助推社區報告的最佳方式是什麼?

+3

問題是什麼?我懷疑這是更好的針對助推社區 - 看起來像一個錯誤。 – marko

+0

@Marko看起來像你的寫,因爲手動添加仿真移動語義使代碼編譯。 – mark

回答

3

這是一個限制到仿真在C++ 03(from here):

宏BOOST_COPYABLE_AND_MOVABLE需要定義副本 構造爲copyable_and_movable取入一個非const參數 C++ 03編譯器:

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

由於產生拷貝構造的非const過載, 編譯器生成的包含 copyable_and_movable的類的賦值操作符將獲得非const拷貝構造函數重載, ,這肯定會讓用戶感到驚訝。 此限制強制用戶在所有擁有可複製和可移動類別的類中定義複製副本 的常量版本,其中 在某些情況下可能會令人厭煩

在你的情況boost::container::vector<data> v_;使用BOOST_COPYABLE_AND_MOVABLE(vector)因此錯誤。