不可复制的STL分配器

4
我想创建一个不可复制的分配器(C++14),它只分配一个固定大小的内存块,供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

3个回答

3

2
这是不可能的。来自 [container.requirements.general]/8 的内容:

[...] 对于这些容器类型,所有其他构造函数都接受一个 const allocator_­type& 参数。[注意:如果构造函数的调用使用可选分配器参数的默认值,则分配器类型必须支持值初始化。 —— end note] 在每个容器对象的生存期内或直到替换分配器为止,这些构造函数和所有成员函数执行的任何内存分配和元素构造都使用此分配器的副本。

我强调了一下

因此,您不能将只能移动的分配器传递给任何接受分配器的容器构造函数。

你是一个分配器。 - Hatted Rooster
@SombreroChicken 什么? - NathanOliver

1

你说:我想要[...]防止用户意外分配内存。

但是你提出的解决方案,如其他答案所述,我想要防止分配器(因此也包括向量)可复制,是不可行的。从你的问题中可以看出,它看起来像一个XY问题

其他人已经回答了你的尝试解决方案。所以我只关注问题本身。因为有可能编写符合标准的分配器来满足你的需求:防止用户意外分配内存。

有许多替代实现可以满足你的需求。但我不知道你具体在寻找什么,因此我提供以下示例,可以根据allocator.requirements中的要求进行调整:

const size_t buffer_size = 4096;
unsigned char buffer[buffer_size];
void* _sbuffer = buffer; //or atomic



template<class T>
class allocator{
   void* buffer = exchange(_sbuffer,nullptr);//could be done atomically
   bool allocatable=buffer?true:false;

   public:

   using value_type = T;

   T* allocate(size_t n){
      if (n>buffer_size || !allocatable) throw std::bad_alloc{};
      allocatable=false;
      return static_cast<T*>(buffer);
      }
   void deallocate(T*,size_t){
      if (buffer) allocatable=true;
      }
   //Here the intersting part:
   allocator select_on_container_copy_construction(){
      return allocator{};
      }

   allocator() =default;

   //this copy constructor is only used internaly
   //but will not be used to propagate the allocator
   //from one container object to an other 
   //(see select_on_container_copy_construction)
   allocator(const allocator& other) =default;

   allocator(allocator&& other)
     :buffer{exchange(other.buffer,nullptr)}
     ,allocatable{exchange(other.allocatable,false)}
     {}
   allocator& operator=(const allocator&) =delete;
   allocator& operator=(allocator&& other){
      buffer=exchange(other.buffer,nullptr);
      allocatable=exchange(other.allocatable,false);
      return *this;
      }

   using propagate_on_container_copy_assignment = false_type;
   using propagate_on_container_move_assignment = true_type;
   using propagate_on_container_swap = true_type;


   //any allocator can deallocate memory provided by an other
   static constexpr bool is_always_equal = true;

   friend bool operator==(const allocator&,const allocator&){
       return true;
       }

   friend bool operator!=(const allocator&,const allocator&){
       return false;
       }
   };

coliru上的演示

它很脆弱,因为如果分配器在容器外部构建,然后进行复制构造并且这些副本稍后用于初始化容器... 您可能会遇到实现定义的行为,例如对于libstdc++,您可以将危险的构造函数声明为私有:

template<class T>
struct allocator{
   /*...*/
   friend std::_Vector_base<T,allocator>;
   friend std::allocator_traits<allocator>;
   private:
   allocator() =default;
   allocator(const allocator& other) =default;
   public:/*...*/
   };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接