std :: vector< char>的自定义分配器被忽略 [英] Custom allocator for std::vector<char> is ignored

查看:101
本文介绍了std :: vector< char>的自定义分配器被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对 std :: vector< char> 使用自定义分配器,但是我注意到 std :: vector 不需要/使用我的分配器中的任何成员函数。

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. 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?
}

我正在尝试使用在线C ++ 11编译器(LiveWorkSpace ),提供g ++ 4.7.2、4.8和4.6.3。

I was trying the above program with an online C++11 compiler (LiveWorkSpace), providing g++ 4.7.2, 4.8 and 4.6.3.

基本上 allocate(),<$ c没有定义$ c> deallocate(), construct() destroy()在我的分配器中,程序仍会编译并且所有元素都将初始化为0。

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.

推荐答案

GCC标准库将始终重新绑定提供的分配器,因此在内部它会执行以下操作(在C ++ 03中):

The GCC standard library will always rebind the supplied allocator so internally it does something like this (in C++03):

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

(在C ++ 11中,它使用 allocator_traits ,但是在这种情况下,结果是相同的。)

(In C++11 it uses allocator_traits but in this case the result is the same.)

然后,向量在内部存储了该类型的对象并将其用于所有(取消)分配。

The vector then stores an object of that type internally and uses it for all (de)allocation.

由于您尚未在分配器中定义 rebind 成员模板,因此您刚刚从基类中重新声明了一个,重新绑定的结果是 std :: allocator< value_type> 而不是您自己的类型。 std :: allocator 当然提供了所有这些功能,因此无论您是否按自己的类型定义它们,这些都是所使用的功能。

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. 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.

您可以通过使用alloc :: rebind; 将其添加到的分配器接口中来修复它,从而使 vector 内部存储并使用 A

You can fix it by adding this to your allocator intead of using alloc::rebind; so that vector stores and uses an A internally:

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

N.B。这仅适用于 vector ,因为 vector 并不一定需要重新绑定分配器(要求用户实例化具有 allocator< value_type> 的模板,但是GCC的 vector 仍然重新绑定,因此,如果用户实例化 vector< int,std :: allocator< char>> 仍然有效。)对于基于节点的容器,例如 std :: set 分配器必须是可以反弹的模板,因为容器需要分配其内部节点类型,而不是 value_type ,因此它需要 Alloc ::重新绑定< internal_node_type> :: other 才有效。

N.B. 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.

这篇关于std :: vector&lt; char&gt;的自定义分配器被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆