【问题标题】:Non-Copyable STL Allocator不可复制的 STL 分配器
【发布时间】:2019-05-06 02:58:56
【问题描述】:

我想创建一个不可复制的分配器(在 C++14 中),它只分配一个 std::vector 可以使用的固定内存块。我想防止分配器(以及向量)可复制,以防止用户意外分配内存。分配器只能与std::vectorstd::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) { }

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

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

完整的代码示例可以在这里找到:https://onlinegdb.com/ByqXwQ4k4

【问题讨论】:

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


    【解决方案1】:

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

    A a1(a)
    A a1 = a
    

    复制构造a1 使得a1 == a。不抛出异常。 (注:每个Allocator也满足CopyConstructible

    【讨论】:

      【解决方案2】:

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

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

      强调我的

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

      【讨论】:

      • 你是一个分配者
      • @SombreroChicken Hun?
      【解决方案3】:

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

      但是您提出的解决方案我想防止分配器(因此也包括向量)被复制,正如其他答案中所说的那样是不可行的。正如它所写的那样,您的问题看起来像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:/*...*/
         };
      

      【讨论】:

        猜你喜欢
        • 2013-04-08
        • 2011-10-23
        • 2010-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 2010-09-07
        • 2014-12-07
        相关资源
        最近更新 更多