stl allocator

以下文件目录均在/usr/include/c++/4.8/中。
bits/allocator.h文件中定义了stl默认使用的allocator。
line46: #include <bits/c++allocator.h> //Define the base class to std::allocator.

bits/c++allocator.h 中有如下代码:

#include <ext/new_allocator.h>

namespace std {
  // An alias to the base class for std::allocator
  // Used to set the std::allocator base class to _gnu_cxx::new_allocator
  template<typename _Tp>
  using __allocate_base = __gnu_cxx::new_allocator<_Tp>;
}

接下来就可以在<ext/new_allocator.h> 查看new_allocator 类的具体实现。

new_allocator类中比较令人困惑的是成员rebind的实现:

template<typename _Tp1>
struct rebind { using other = new_allocator<_Tp1>; }

查阅The C++ programming languageP998:
The curious rebind template is an archaic(古老) alias. It should have been:

template<typename U>
using other = allocator<U>;

However, allocator was defined before such aliases were supported by C++. It is provided to alllow an allocator to allocate objects of arbitrary type. Consider:
using Link_alloc = typename A::template rebind<Link>::other;
if A is an allocator, then rebind<Link>::other is an alias for allocator<Link>. For example:

template<typename T, typename A = allocator<T>>
class list {
private:
  class Link { };
  using Link_alloc = typename A::template rebind<Link>::other; // allocator<Link>
  Link_alloc a; // link allocator
  A alloc; // list allocator
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值