开发者

stl allocator, copy constructor of other type, rebind

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?


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.

The code looks similar to:

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;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜