首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

stl中弄个rebind出来是什么意思

2012-11-07 
stl中搞个rebind出来是什么意思?C/C++ codetemplateclass _Elem,class _Traits,class _Axclass basic_st

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

C/C++ code
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有什么必要?

[解决办法]
举个例子:我想要一个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>;仔细想想????处该添啥?
};

热点排行