STL解读-rebind的目的就是再定义一个struct (allocator), 这个allocator能分配 _Other类型的元素。

STL源文件:

文件:c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xmemory

行:103-107

template<class _Other>
struct rebind
{ // convert an allocator<_Ty> to an allocator <_Other>
typedef allocator<_Other> other;
};


rebind的目的就是定义一个struct (allocator), 这个allocator能分配 _Other类型的元素。

用途:

如std::list<int>的allocator,默认分配的元素类型是int,通过如下定义

见文件:C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\list

typename _Alloc::template rebind<_Node>::other
_Alnod; // allocator object for nodes
};


就可以用_Alnod为list的实际元素_Node 分配空间了,

_Node的定义:

typedef _Node *_Nodeptr; // _Node allocator must have ordinary pointers

struct _Node
{ // list node
_Node()
{ // default constructor, do nothing
}


_Nodeptr _Next; // successor node, or first element if head
_Nodeptr _Prev; // predecessor node, or last element if head
_Ty _Myval; // the stored value, unused if head
};


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值