6

vector.reserve()大量の要素を挿入する前に呼び出すことをお勧めします。多数のconst要素を に入れたいという状況に遭遇しましたvector。ただし、を呼び出すとreserve()、コンパイラ エラーがスローされます。

問題を再現する次のコードを検討してください。

#include <vector>

int main()
{
    std::vector<const int> vec;
    vec.reserve(2);
}

これにより、次のような大きなコンパイラ エラーが発生します。

/usr/include/x86_64-linux-gnu/c++/4.8/bits/c++allocator.h:33:0 からインクルードされたファイルで、
                 /usr/include/c++/4.8/bits/allocator.h:46 から、
                 /usr/include/c++/4.8/vector:61 から、
                 vecreserve.cpp:1 から:
/usr/include/c++/4.8/ext/new_allocator.h: 'struct __gnu_cxx::new_allocator' のインスタンス化:
/usr/include/c++/4.8/bits/allocator.h:92:11: 'class std::allocator' から必要
/usr/include/c++/4.8/bits/alloc_traits.h:90:43: 'struct std::allocator_traits >' から必要
/usr/include/c++/4.8/ext/alloc_traits.h:121:10: 'struct __gnu_cxx::__alloc_traits >' から必要です
/usr/include/c++/4.8/bits/stl_vector.h:75:28: 'struct std::_Vector_base >' から必要
/usr/include/c++/4.8/bits/stl_vector.h:210:11: 'class std::vector' から必要
vecreserve.cpp:5:26: ここから必要
/usr/include/c++/4.8/ext/new_allocator.h:93:7: エラー: 'const _Tp* __gnu_cxx::new_allocator::address(__gnu_cxx::new_allocator::const_reference) const [with _Tp = const int; __gnu_cxx::new_allocator::const_pointer = const int*; __gnu_cxx::new_allocator::const_reference = const int&]' はオーバーロードできません
       address(const_reference __x) const _GLIBCXX_NOEXCEPT
       ^
/usr/include/c++/4.8/ext/new_allocator.h:89:7: エラー: with '_Tp* __gnu_cxx::new_allocator::address(__gnu_cxx::new_allocator::reference) const [with _Tp = const int; __gnu_cxx::new_allocator::pointer = const int*; __gnu_cxx::new_allocator::reference = const int&]'
       アドレス(参照 __x) const _GLIBCXX_NOEXCEPT
       ^
/usr/include/c++/4.8/ext/new_allocator.h: 'void __gnu_cxx::new_allocator::deallocate(__gnu_cxx::new_allocator::pointer, __gnu_cxx::new_allocator::size_type) のインスタンス化 [with _Tp = const int ; __gnu_cxx::new_allocator::pointer = const int*; __gnu_cxx::new_allocator::size_type = long unsigned int]':
/usr/include/c++/4.8/bits/stl_vector.h:174:4: 'void std::_Vector_base::_M_deallocate(std::_Vector_base::pointer, std::size_t) から必要 [with _Tp = const int ; _Alloc = std::アロケーター; std::_Vector_base::pointer = const int*; std::size_t = long unsigned int]'
/usr/include/c++/4.8/bits/vector.tcc:80:28: 'void std::vector::reserve(std::vector::size_type) [with _Tp = const int; から必要です。_Alloc = std::アロケータ; std::vector::size_type = long unsigned int]'
vecreserve.cpp:6:16: ここから必要
/usr/include/c++/4.8/ext/new_allocator.h:110:30: エラー: 'const void*' から 'void*' への無効な変換 [-fpermissive]
       { ::operator delete(__p); }
                              ^
/usr/include/c++/4.8/ext/new_allocator.h:33:0 からインクルードされたファイルで、
                 /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++allocator.h:33 から、
                 /usr/include/c++/4.8/bits/allocator.h:46 から、
                 /usr/include/c++/4.8/vector:61 から、
                 vecreserve.cpp:1 から:
/usr/include/c++/4.8/new:95:6: エラー: 'void operator delete(void*)' の引数 1 を初期化しています [-fpermissive]
 void 演算子 delete(void*) _GLIBCXX_USE_NOEXCEPT
      ^
/usr/include/c++/4.8/vector:60:0 からインクルードされたファイルで、
                 vecreserve.cpp:1 から:
/usr/include/c++/4.8/bits/stl_algobase.h: '_OI std::__copy_move_a(_II, _II, _OI) のインスタンス化 [with bool _IsMove = true; _II = const int*; _OI = const int*]':
/usr/include/c++/4.8/bits/stl_algobase.h:428:38: '_OI から必要 std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = true; _II = const int*; _OI = const int*]'
/usr/include/c++/4.8/bits/stl_algobase.h:460:17: '_OI std::copy(_II, _II, _OI) [with _II = std::move_iterator; から必要です。_OI = const int*]'
/usr/include/c++/4.8/bits/stl_uninitialized.h:93:53: 'static _ForwardIterator std::__uninitialized_copy::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; から必要です。_ForwardIterator = const int*]'
/usr/include/c++/4.8/bits/stl_uninitialized.h:117:41: '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; から必要です。_ForwardIterator = const int*]'
/usr/include/c++/4.8/bits/stl_uninitialized.h:258:63: '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator&) から必要 [with _InputIterator = std::move_iterator; _ForwardIterator = const int*; _Tp = const int]'
/usr/include/c++/4.8/bits/stl_vector.h:1142:29: 'std::vector::pointer から必要 std::vector::_M_allocate_and_copy(std::vector::size_type, _ForwardIterator, _ForwardIterator) [ with _ForwardIterator = std::move_iterator; _Tp = const int; _Alloc = std::アロケータ; std::vector::pointer = const int*; std::vector::size_type = long unsigned int]'
/usr/include/c++/4.8/bits/vector.tcc:75:70: 'void std::vector::reserve(std::vector::size_type) [with _Tp = const int; から必要です。_Alloc = std::アロケータ; std::vector::size_type = long unsigned int]'
vecreserve.cpp:6:16: ここから必要
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: エラー: 'std::__copy_move::__copy_m(const int*&, const int*&, const int*& への呼び出しに一致する関数がありません)'
                        _Category>::__copy_m(__first, __last, __result);
                                                                      ^
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: 注: 候補は:
/usr/include/c++/4.8/bits/stl_algobase.h:368:9: 注意: テンプレート static _Tp* std::__copy_move::__copy_m(const _Tp*, const _Tp*, _Tp*) [with _Tp = _Tp; bool _IsMove = true]
         __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
         ^
/usr/include/c++/4.8/bits/stl_algobase.h:368:9: 注: テンプレート引数の推定/置換に失敗しました:
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: 注: パラメーター '_Tp' ('int' と 'const int') の競合する型を推測しました
                        _Category>::__copy_m(__first, __last, __result);
                                                                      ^

なぜこれが起こるのですか?要素reserveの avectorを呼び出すべきではありませんか? constもしそうなら、なぜですか?

4

2 に答える 2