[英]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.