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&]' 不能重载
       地址(const_reference __x) const _GLIBCXX_NOEXCEPT
       ^
/usr/include/c++/4.8/ext/new_allocator.h:89:7: error: 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::allocator; 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::allocator; 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]
       { ::运算符删除(__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]
 无效运算符删除(无效*)_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 = 常量 int*; _OI = 常量 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 = 常量 int*; _OI = 常量 int*]'
/usr/include/c++/4.8/bits/stl_algobase.h:460:17: 来自 '_OI std::copy(_II, _II, _OI) [with _II = std::move_iterator; _OI = 常量 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 = 常量 int*; _Tp = 常量 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) [与 _ForwardIterator = std::move_iterator; _Tp = 常量 int; _Alloc = std::allocator; 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::allocator; 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: 注意:模板静态 _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一个vector元素const吗?如果是这样,为什么不呢?

4

2 回答 2

10

您违反了该类规定的元素的要求vector。元素必须是CopyAssignable

于 2013-09-06T07:37:18.083 回答
3

const int不是放入任何标准容器的有效类型,因为它不可分配。

于 2013-09-06T07:36:39.573 回答