std::vector
使用。我希望防止分配器(因此也包括向量)可复制,以防用户意外分配内存。该分配器仅用于std::vector
或者std::string
。因此,我的分配器具有以下方式的复制构造函数:
static_allocator(const static_allocator<T>&) = delete;
在调用时:
std::vector<int, static_allocator<int>> vvv(static_allocator<int>(3));
我遇到了如下编译错误:
/usr/include/c++/5/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl(const _Tp_alloc_type&) [with _Tp = int; _Alloc = static_allocator<int>; std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type = static_allocator<int>]’:
/usr/include/c++/5/bits/stl_vector.h:128:20: required from ‘std::_Vector_base<_Tp, _Alloc>::_Vector_base(const allocator_type&) [with _Tp = int; _Alloc = static_allocator<int>; std::_Vector_base<_Tp, _Alloc>::allocator_type = static_allocator<int>]’
/usr/include/c++/5/bits/stl_vector.h:265:18: required from ‘std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = int; _Alloc = static_allocator<int>; std::vector<_Tp, _Alloc>::allocator_type = static_allocator<int>]’
错误似乎来自于在
stl_vector.h:265
中没有定义移动语义分配器的构造函数:/**
* @brief Creates a %vector with no elements.
* @param __a An allocator object.
*/
explicit
vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
: _Base(__a) { }
虽然更深处的代码实际上支持rvalue分配器,但由于上面提到的构造函数使用引用获取了rvalue,因此这些分配器并未被调用。
这是C++14中缺少的功能还是我漏了什么选项?另外,在构造向量时复制分配器似乎没有明显的原因。
完整的代码示例可以在此找到:https://onlinegdb.com/ByqXwQ4k4