- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚完成了一个关于覆盖 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; }
};
最佳答案
My question is how does an allocator recipient, e.g. std::set, work if its received allocator only optionally implements functions and types?
如果你看the allocator concecpt :
Some requirements are optional: the template std::allocator_traits supplies the default implementations for all optional requirements, and all standard library containers and other allocator-aware classes access the allocator through std::allocator_traits, not directly.
这也是为什么有这么多东西是可选的 - 大多数分配器实现实际上不需要更改它们,所以何必呢?假设您有一个新的内存重组算法的想法,为什么需要定义 pointer
?
Or is the reasoning that one would be alerted to the need to implement these optional functions by a compilation error?
不是,分配器的概念是well defined .它指定了您必须提供什么,以及您可以提供什么。无需依赖编译错误。
您可以在 the standard 中找到要求, $17.6.3.5.
关于c++ - 为什么很多 Allocator 函数是可选的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39405014/
N3485 20.6.9.1 [allocator.members]/1 说: Calls to these functions that allocate or deallocate a parti
我想编写一个调用 createHook() 的自定义分配器在对象构造和对称之后 destroyHook()就在对象销毁之前。我以这种方式使用我的分配器: class Object {}; class
我正在用 C++ 重新创建一个链表,并且在重载 += 运算符时得到了一个错误的指针。我想我只是以错误的方式使用了分配器,但我可能是错的。 这里是上下文: void MyLinkedList::oper
Allocator concept和 std::allocator_traits没有说明 allocate 是否会抛出。 所以当我使用分配器编写容器时,如何知道是检查返回类型还是使用 catch? 最
C++20 删除了 construct()和 destruct()成员(member)来自 std::allocator .我应该如何构造通过 std::allocator::allocate() 分
这个问题听起来可能相当初级,但这是我与另一位合作开发人员的辩论。 我注意在可能的地方分配堆栈,而不是堆分配它们。他在和我说话并看着我的肩膀并评论说没有必要,因为他们在表现方面是一样的。 我一直认为堆栈
这个问题听起来可能相当初级,但这是我与另一位合作开发者的争论。 我一直在尽可能地堆栈分配东西,而不是堆分配它们。他一边跟我说话,一边看着我,并评论说没有必要,因为它们在性能方面是相同的。 我一直认为堆
在 Java 程序中,当需要分配数千个相似大小的对象时,最好(在我看来)有一个“池”(这是一个单一的分配),其中包含可以从中提取的保留项目需要的时候。这个单一的大分配不会像数千个较小的分配那样使堆碎片
我正在尝试使用 TBB 来提升使用 OpenCV 的计算机视觉项目的性能。这是代码中给出访问冲突的部分。 #include #include "opencv2/objdetect/objdetect
我对一个问题有疑问,特别是关于 this 的问题回答。 有一部分留给读者作为练习(这本身不是问题),特别是 Jonathan Wakely(答案的作者)说: This code asserts tha
Allocator concept和 std::allocator_traits不要说当分配失败时 allocate 会做什么——它会返回 nullptr 还是抛出异常? 当我使用标准分配器 API
我有充分的理由不应该做这样的事情吗?示例: 我有一个类(class)MyClass。在那里我有这个实现: - (id)copyWithZone:(NSZone*)zone { MyClass
相关但不重复:请参阅此答案的底部,在单击此问题下方的“关闭”按钮之前,我解决了您可能想要声明的重复项。 自动生成 ROS (Robot Operating System) message C++ 头文
据我所知std::allocator::construct在旧版本的 C++ 上仅需要两个参数;第一个是指向原始的、未构造的内存的指针,我们要在其中构造 T 类型的对象。第二个是用于初始化该对象的元素
40个不同的分配函数给40个不同的调用点 void f00(size_t sz) { void* ptr = malloc(sz); free(ptr); } void f01(size_t sz)
我在使用 RenderScript 时一直遇到内存管理问题,所以我认为由于 Allocation.createFromBitmap()/createTyped() 消耗内存,Allocation.de
我正在尝试使用 valgrind 跟踪段错误。我从 valgrind 收到以下消息: ==3683== Conditional jump or move depends on uninitialise
实际上,我正在尝试创建一个包含 n 个多媒体文件(包括图像和视频)的应用程序。我的应用程序大小约为 34MB,我的 Assets 大小约为 60mb。当我在普通设备上加载应用程序时,我们没有遇到任何问
STL 容器有一个模板参数可以选择自定义分配器。花了一段时间,但我想我明白它是如何工作的。不知何故,它并不是很好,因为给定的分配器类型没有直接使用,而是反弹到另一种类型的分配器。我终于可以使用它了。
new int[0]在 C++ 中是允许的,但 std::allocator().allocate(0)定义好? 更一般地说,所有分配器都必须接受 0作为参数分配? 编辑: 阅读答案后,我测试了 Vi
我是一名优秀的程序员,十分优秀!