STL 分配器需要以下构造函数形式 (20.1.5):X a(b);
的要求是Y(a) == b;
在标准实现中,这意味着并且实现为:
template<class U> allocator( const allocator<U> & o ) throw()
我无法理解为什么存在这个要求。我知道分配器应该是静态的(没有任何状态),但是为什么你能够像这样转换它们呢?
为了允许从其他分配器进行构造,因为容器需要使用与您指定的分配器类型不同的分配器类型。例如,列表和映射分配其内部节点类型,而不是它们公开的 value_type。
该代码看起来类似于:
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;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)