不可复制的STL分配器

时间:2018-12-04 16:11:24

标签: c++ c++11 memory-management allocator

我想创建一个不可复制的分配器(在C ++ 14中),该分配器仅分配<?php class A { /** @return static */ public function methodA(): A; } class B { /** @return static */ public function methodB(): B; } (new B())->methodB()->methodA()->methodB(); 可以使用的固定内存块。我想防止分配器(以及向量)也可复制,以防止用户意外分配内存。分配器只能与std::vectorstd::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

虽然更深入的代码实际上支持rvalue分配器,但未调用这些分配器,因为rvalue由上述构造函数通过引用获取。

这是C ++ 14中缺少的功能还是缺少某些选项?奇怪的是,在构造向量时没有明显的原因就复制了分配器。

完整的代码示例可在此处找到:https://onlinegdb.com/ByqXwQ4k4

3 个答案:

答案 0 :(得分:2)

根据requirements for an Allocator type,您的分配器类型需要满足CopyConstructible,这意味着您无法删除副本副本:

A a1(a)
A a1 = a
     

复制a1a1 == a。不抛出异常。   (请注意:每个Allocator也满足CopyConstructible

答案 1 :(得分:2)

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

  

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

强调我的

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

答案 2 :(得分:1)

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

但是您提出的解决方案我想防止分配器(以及向量)也可复制,因为在其他答案中没有提到。在撰写本文时,您的问题看起来像是XY problem

其他人可以回答您的尝试解决方案。因此,我将仅关注该问题。因为可以编写一个符合标准的分配器来满足您的需求:防止用户意外分配内存。

有许多可能适合您的替代实现。但是我不知道您要寻找的是什么,因此我在下面提出一个可以根据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:/*...*/
   };
相关问题