allocator::rebind详解

本文深入探讨了C++ STL中rebind机制的本质与作用,解释了如何通过rebind实现在不同类型的对象间应用相同的内存分配策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

rebind的本质应该这么说:给定了类型T的分配器Allocator=allocator<T>,现在想根据相同的策略得到另外一个类型U的分配器allocator<U>,那么allocator<U>=allocator<T>::Rebind<U>::other.

之所以要提供rebind接口,是因为容器只知道模板参数名Allocator,而不知其具体实现,容器只知道这样三个事情:
1、Alocator是T的分配器,但其内部实现策略是什么容器并不关心(可能是std::allocator<T>,也可能是myallocator<T>)。换句话说,容器并不知道allocator模板名。
2、类型T和类型U在逻辑上是相关的,比如在链表中,数据类型T和结点类型Node<T>是有联系的。
3、容器希望按照和T一样的策略(具体的说就是相同的allocator模板名)来分配U类型的对象。
这时rebind的作用就体现出来了,标准中规定
对一个allocator<T>,和一个类型U,必须有allotor<T>::rebind<U>::other=allocator<U>,这样,
容器就可以得到U的分配器,
也就是说,rebind的本质应该是:,
对allotor<T>::rebind<U>,T和U的分配器必须是同一个模板名。这个模板可以是std::allocator,也可以是用户自定义的分配器模板Myallocator,自然,为了使自己的分配器可以作为容器的模板参数,Myallocator中也必须定义rebind成员,且其实现必为
template<typename U>
rebind{
 typedef Myallocator<U> other;
};
因此,同族的分配器指的是具有相同模板名的一组分配器。
如std::allcoator<T>和std::allcoator<U>是同族的,你可以把std::allcoator<T>::rebind<U>::other看成std::allcoator<U>。
Myallocator<T>和Myallocator<U>是同族的,你可以把Myallcoator<T>::rebind<U>::other看成Myallcoator<U>。

但allcoator<T>和Myallocator<U>就不是同族的.

 

Alocator 是内存分配器,STL在这个层面上进行开放是为了让用户/程序员有权选择不同的内存分配器。举例来说,Allocator_A和Allocator_B在内存分配方式上可能是不一样的,也就是说Allocator_A<int>和Allocator_B<int>所分配的内存很有可能是不一样的。  

假如有一个容器类MyVector, 它用的是Allocator_A<int>内存分配器,这个容器类很有可能需要double类型的分配器,而且要求对int和double类型的内存分配策略是一样的,这时rebind的意义就体现出来了。


总之一句话,rebind实现了对不同类型使用同一种内存分配策略的要求。


转自http://bbs.youkuaiyun.com/topics/200079053

#ifndef ALLOCATOR_HPP #define ALLOCATOR_HPP #include <stddef.h> #include <limits> template <typename T> class Allocator { public: typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; //Allocator::rebind<T2>::other template <typename V> struct rebind { typedef Allocator<V> other; }; pointer address(reference value) const { return &value; } const_pointer address(const_reference value) const { return &value; } Allocator() throw() { } Allocator(const Allocator &) throw() { } //不同类型的allcator可以相互复制 template <typename V> Allocator(const Allocator<V> &other) { } ~Allocator() throw() { } //最多可以分配的数目 size_type max_size() const throw() { return std::numeric_limits<size_type>::max() / sizeof(T); } //分配内存,返回该类型的指针 pointer allocate(size_type num) { return (pointer)(::operator new(num * sizeof(T))); } //执行构造函数,构建一个对象 void construct(pointer p, const T &value) { new ((void*)p) T(value); } //销毁对象 void destroy(pointer p) { p->~T(); } //释放内存 void deallocate(pointer p, size_type num) { ::operator delete((void *)p); } }; template <typename T, typename V> bool operator==(const Allocator<T> &, const Allocator<V> &) throw() { return true; } template <typename T, typename V> bool operator!=(const Allocator<T> &, const Allocator<V> &) throw() { return false; } #endif讲解这段代码
最新发布
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值