stl中搞个rebind出来是什么意思?

youyilu2 2012-10-25 11:08:25

template<class _Elem,
class _Traits,
class _Ax>
class basic_string
: public _String_val<_Elem, _Ax>
{
...
typedef _String_val<_Elem, _Ax> _Mybase;
typedef typename _Mybase::_Alty _Alloc;
...


template<class _Ty,
class _Alloc>
class _String_val
: public _String_base
{
...
typedef typename _Alloc::template
rebind<_Ty>::other _Alty;
...


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



这个rebind有什么必要?
...全文
174 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
npuhuxl 2012-10-26
  • 打赏
  • 举报
回复
其实最重要的原因是微软的编译器不支持template template (VS2010不知道支不支持)

template<typenname T, template <typename> class Alloc>
class list
{
Alloc<node> alloc_;
};

好像语法是这样的,可以google一下
npuhuxl 2012-10-25
  • 打赏
  • 举报
回复
举个例子:我想要一个list<int, alloc<int> >
template<typename T, typename Alloc>
class list
{
private:
struct node
{
T val;
node* next;
};

Alloc::rebind<node> alloc_; // 用这个为list node分配,而不是alloc<int>,这样node就不用暴露到外面了,而且不这样的话,你得传入一个alloc<list<int, ????>::node>;仔细想想????处该添啥?
};

64,641

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧