stl allocator,其他类型的复制构造函数,重新绑定

edA*_*a-y 6 c++

STL分配器需要这个构造函数形式(20.1.5):X a(b);具有要求Y(a) == b;

在标准实现中,这意味着,并实现为:

  template<class U> allocator( const allocator<U> & o ) throw()
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么存在此要求.我知道分配器应该是静态的(没有任何状态),但为什么你能够像这样转换它们呢?

小智 6

允许来自其他分配器的构造,因为容器需要使用与您指定的不同的分配器类型.例如,列表和映射分配其内部节点类型而不是它们公开的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;
};
Run Code Online (Sandbox Code Playgroud)

  • @edA:我不认为在计算向后兼容性的损失上总体上会更好的系统上没有达成共识。但是,公平地说,我没有遵循向委员会提出的建议,而是一直专注于0x草案。另外,当前的系统确实可以用于大多数用途-您可能对[EASTL doc](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html)感兴趣。 (2认同)