为什么许多Allocator函数是可选的
Why are many Allocator functions optional?
我刚刚完成了一个关于重写operator new和delete的个人项目,并在此过程中学习了有关分配器类的知识。在阅读了包括cppreference.com在内的一些在线参考资料后,我注意到许多函数被描述为可选的。
我的问题是,如果接收到的分配器只能选择性地实现函数和类型,那么分配器接收者(例如std::set
)如何工作?
如果要求分配器从某些基类派生,这些基类对所有函数都有默认实现,我可以理解,但似乎对分配器没有继承要求。
还是因为编译错误而提醒人们需要实现这些可选函数?
作为参考,这是我第一次尝试使用分配器,我将它用作std::set
的第三个模板参数。我完成了一个现有的示例,因此我相信我实现的大部分内容可能是不必要的,但我还不明白如果将来选择将分配器与其他STL容器一起使用,如何判断是否必要。除非,再一次,期望是根据编译错误来找出它…?
template <typename T>
class Allocator // Custom allocator for use by AllocSet::mAllocatedPtrs to avoid infinite recursion
{ // (because inserting an element in std::set calls ::operator new, which is overridden to add to AllocSet::mAllocatedPtrs).
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef const T* const_pointer;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template <typename U>
struct rebind { typedef Allocator<U> other; };
Allocator() { }
template <typename U> Allocator(const Allocator<U>&) { }
virtual ~Allocator() { }
pointer allocate(size_t numT) { return (T*)(malloc(numT * sizeof(T))); }
void deallocate(pointer p, size_type st) { free(p); }
size_type max_size() { return size_type(-1); }
reference operator=(const_reference) { return *this; }
template <typename U> reference operator=(const Allocator<U>&) { return *this; }
template <typename U> bool operator==(const Allocator<U>&) { return true; }
template <typename U> bool operator!=(const Allocator<U>&) { return false; }
pointer address(reference r) { return &r; }
const_pointer address(const_reference r) { return &r; }
};
我的问题是,如果接收到的分配器只能选择性地实现函数和类型,那么分配器接收者(例如std::set)如何工作?
如果你看看分配器的概念:
有些需求是可选的:模板std::allocator_traits提供了所有可选需求的默认实现,所有标准库容器和其他分配器感知类都通过std::allocator_traits访问分配器,而不是直接访问。
这也是为什么这么多东西是可选的原因——大多数分配器的实现实际上不需要改变它们,所以为什么要这么麻烦呢?假设你有一个新的内存重组算法的想法,为什么你需要定义pointer
?
还是因为编译错误而提醒人们需要实现这些可选函数?
不,分配器的概念定义得很好。它指定您必须提供什么,以及您可以提供什么。没有必要依赖于编译错误。
您可以在标准$17.6.3.5中找到这些要求。
- 函数在许多数字上转换为基数 1 时减去 2?
- C++函数调用与许多点运算符
- 在constexpr函数中插入许多模板
- 递归中的许多函数调用
- 重构许多函数以优雅地接收任何类型的参数(模板<any>)
- 我如何实现递归函数的模板,该模板允许C 中的许多不确定数据类型的参数
- 在许多不同的类中调用相同的函数
- C++设计:重载/重写许多函数,如何清理
- 许多函数的相同模板
- 为具有许多友元函数c++的模板类(2d动态)创建析构函数
- 我应该将许多函数中使用的变量声明为成员变量吗
- 递归函数中的许多参数会导致性能问题吗
- 许多函数的单个DLL入口点
- 传递许多函数并将其所有结果存储在元组中
- Qt中许多Windows函数的未解决的链接器错误
- 如何启动具有许多布尔参数的函数模板,而不使用 2^n if 语句
- C++类中的许多成员函数对性能的影响
- 具有相同构造函数的许多类
- 对许多函数调用进行错误检查
- 类和许多可以访问其私有数据的函数