简体   繁体   English

将忽略std :: vector <char>的自定义分配器

[英]Custom allocator for std::vector<char> is ignored

I was trying to use a custom allocator for std::vector<char> , but I noticed that std::vector does not need/use any of the member functions from my allocator. 我试图为std::vector<char>使用自定义分配器,但我注意到std::vector不需要/使用我的allocator中的任何成员函数。 How is this possible? 这怎么可能?

#include <vector>

struct A : private std::allocator<char> {
   typedef std::allocator<char> alloc;
   using alloc::value_type;
   using alloc::pointer;
   using alloc::const_pointer;
   using alloc::difference_type;
   using alloc::size_type;
   using alloc::rebind;
   // member functions have been removed, since the program compiles without them
};

int main() {
    std::vector<char, A> v;
    v.resize(4000);
    for (auto& c : v)
      if (c)
         return 1; // never happens in my environment
   return 0; // all elements initialized to 0. How is this possible?
}

I was trying the above program with an online C++11 compiler (LiveWorkSpace), providing g++ 4.7.2, 4.8 and 4.6.3. 我正在使用在线C ++ 11编译器(LiveWorkSpace)尝试上述程序,提供g ++ 4.7.2,4.8和4.6.3。

Basically allocate() , deallocate() , construct() and destroy() are not defined in my allocator, yet the program compiles and all the elements will be initialized to 0. 基本上,在我的分配器中没有定义allocate()deallocate()construct()destroy() ,但是程序编译并且所有元素将被初始化为0。

The GCC standard library will always rebind the supplied allocator so internally it does something like this (in C++03): GCC标准库将始终重新绑定提供的分配器,因此在内部它执行类似的操作(在C ++ 03中):

typedef Alloc::template rebind<value_type>::other _Allocator_type;

(In C++11 it uses allocator_traits but in this case the result is the same.) (在C ++ 11中,它使用allocator_traits但在这种情况下结果是相同的。)

The vector then stores an object of that type internally and uses it for all (de)allocation. 然后,向量在内部存储该类型的对象,并将其用于所有(de)分配。

Since you haven't defined a rebind member template in your allocator, you've just redeclared the one from the base class, the result of the rebinding is std::allocator<value_type> and not your own type. 由于您尚未在分配器中定义rebind成员模板,因此您刚刚从基类重新声明了该模板,重新绑定的结果是std::allocator<value_type>而不是您自己的类型。 std::allocator of course provides all those functions, so those are the ones that are used, whether or not you define them on your own type. 当然, std::allocator提供了所有这些函数,因此无论你是否在自己的类型中定义它们,都会使用它们。

You can fix it by adding this to your allocator intead of using alloc::rebind; 您可以通过将此添加到using alloc::rebind; allocator intead来修复它using alloc::rebind; so that vector stores and uses an A internally: 以便vector A内部存储和使用A

struct A : private std::allocator<char> {
    template<typename U>
      struct rebind {
        typedef A other;
      };

NB this will only work for vector , because vector doesn't strictly need to rebind the allocator (users are required to instantiate the template with allocator<value_type> , but GCC's vector rebinds anyway so that if users instantiate vector<int, std::allocator<char>> it still works.) For node-based containers such as std::set your allocator must be a template that can be rebound, because the container needs to allocate its internal node types, not the value_type , so it needs Alloc::rebind<internal_node_type>::other to be valid. 注意这只适用于vector ,因为vector并不需要重新绑定分配器(用户需要使用allocator<value_type>实例化模板,但是GCC的vector仍会重新绑定,以便用户实例化vector<int, std::allocator<char>>它仍然有效。)对于基于节点的容器,例如std::set你的allocator必须是一个可以反弹的模板,因为容器需要分配它的内部节点类型,而不是value_type ,所以它需要Alloc::rebind<internal_node_type>::other有效。

vector will rebind the allocator. vector将重新绑定分配器。 As you bring it into scope from std::allocator , A::rebind<T>::other will simply be std::allocator<T> . 当你从std::allocator将它带入范围时, A::rebind<T>::other将只是std::allocator<T> So everything works fine. 所以一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std :: vector中的自定义分配器 - Custom allocator in std::vector std :: vector <int,std :: allocator <char >>是否有效? - Is std::vector<int, std::allocator<char> > valid? 带有自定义分配器的 std::vector 与 std::vector - std::vector against std::vector with custom allocator 带有发布的 std::vector&lt;&gt; 的自定义分配器? - Custom allocator for std::vector<> with release? 管理使用新char *创建但使用自定义分配器分配给std :: vector元素的内存块 - Managing memory block created with new char* but allocated to std::vector elements with custom allocator 模板typedef与std :: vector有自定义分配器 - template typedef with std::vector to have custom allocator 如何将带有自定义分配器的std :: vector传递给期望使用std :: allocator的函数? - How to pass an std::vector with custom allocator to a function that expects one with std::allocator? 具有自定义内存分配器的std :: vector的C ++模板 - c++ template for std::vector with custom memory allocator 我可以编写一个自定义分配器来确定std :: vector的重新分配量吗? - Can I write a custom allocator to decide the reallocation amount of std::vector? 使用自定义分配器分配器在std :: vector的末尾使用内存 - Utilize memory past the end of a std::vector using a custom overallocating allocator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM