stl分配器,其他类型的复制构造函数,重新绑定 [英] stl allocator, copy constructor of other type, rebind
问题描述
STL分配器需要这个构造函数形式(20.1.5): X a(b);
,要求 Y = b;
The STL allocators require this constructor form (20.1.5): X a(b);
with the requirement that Y(a) == b;
在标准实现中,这意味着并实现为:
In the standard implementation this implies, and is implemented as:
template<class U> allocator( const allocator<U> & o ) throw()
这个要求存在。我理解分配器应该是静态的(没有任何状态),但为什么你应该能够像这样转换它们?
I'm having trouble understanding why this requirement exists. I understand that allocators should be static (not have any state), but why on earth should you be able to convert them like this?
推荐答案
允许从其他分配器构建,因为容器需要使用不同于您指定的分配器类型。例如,列表和地图分配它们的内部节点类型,而不是它们所暴露的value_type。
To allow construction from other allocators, as containers need to use a different allocator type than you specify. Lists and maps, for example, allocate their internal node type rather than the value_type they expose.
代码类似于:
template<class T, class Alloc=std::allocator<T> >
struct Container {
typedef T value_type;
typedef Alloc allocator_type;
private:
struct Node {/*...*/};
typedef typename Alloc::template rebind<Node>::other RealAllocatorType;
RealAllocatorType allocator;
};
这篇关于stl分配器,其他类型的复制构造函数,重新绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!