是否将shared_ptr的删除器存储在自定义分配器分配的内存中? [英] Is a shared_ptr's deleter stored in memory allocated by the custom allocator?
问题描述
说我有一个shared_ptr
,带有一个自定义分配器和一个自定义删除器.
Say I have a shared_ptr
with a custom allocator and a custom deleter.
我在标准中找不到任何内容讨论删除器的存储位置:它没有说自定义分配器将用于删除器的内存,也没有说它是不会.
I can't find anything in the standard that talks about where the deleter should be stored: it doesn't say that the custom allocator will be used for the deleter's memory, and it doesn't say that it won't be.
这是未指定的,还是我只是缺少一些东西?
Is this unspecified or am I just missing something?
推荐答案
util.smartptr.shared.const/9在C ++ 11中:
util.smartptr.shared.const/9 in C++ 11:
效果:构造一个拥有对象p和删除器d的shared_ptr对象.第二个和第四个构造函数应使用a的副本为内部使用分配内存.
Effects: Constructs a shared_ptr object that owns the object p and the deleter d. The second and fourth constructors shall use a copy of a to allocate memory for internal use.
第二个和第四个构造函数具有以下原型:
The second and fourth constructors have these prototypes:
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
template<class D, class A> shared_ptr(nullptr_t p, D d, A a);
在最新草案中,util.smartptr.shared.const/10等效于我们的目的:
In the latest draft, util.smartptr.shared.const/10 is equivalent for our purpose:
效果:构造一个拥有对象p和删除器d的shared_ptr对象.当T不是数组类型时,第一和第二个构造函数使用p启用shared_from_this.第二和第四构造函数应使用a的副本为内部使用分配内存.如果引发异常,则调用d(p).
Effects: Constructs a shared_ptr object that owns the object p and the deleter d. When T is not an array type, the first and second constructors enable shared_from_this with p. The second and fourth constructors shall use a copy of a to allocate memory for internal use. If an exception is thrown, d(p) is called.
因此,如果需要在分配的内存中分配分配器,则使用分配器.根据当前标准和相关缺陷报告,分配不是强制性的,而是由委员会承担的.
So the allocator is used if there is a need to allocate it in allocated memory. Based on the current standard and at relevant defect reports, allocation is not mandatory but assumed by the committee.
-
尽管
shared_ptr
的接口允许一种实现,其中永远不存在控制块,并且所有shared_ptr
和weak_ptr
都放在链接列表中,但实际上没有这种实现.另外,例如,假设共享use_count
,措词也进行了修改.
Although the interface of
shared_ptr
allows an implementation where there is never a control block and allshared_ptr
andweak_ptr
are put in a linked list, there is no such implementation in practice. Additionally, the wording has been modified assuming, for instance, that theuse_count
is shared.
删除器仅可移动可构造的.因此,shared_ptr
中不可能有多个副本.
The deleter is required to only move constructible. Thus, it is not possible to have several copies in the shared_ptr
.
可以想象有一种实现,该实现将删除器放入专门设计的shared_ptr
中,并在删除特殊的shared_ptr
时将其移动.尽管实现看起来是一致的,但也很奇怪,特别是因为使用计数可能需要一个控制块(使用计数来做相同的事情也许是可能的,但甚至很奇怪).
One can imagine an implementation which puts the deleter in a specially designed shared_ptr
and moves it when it the special shared_ptr
is deleted. While the implementation seems conformant, it is also strange, especially since a control block may be needed for the use count (it is perhaps possible but even weirder to do the same thing with the use count).
我发现了相关的DR: 545 , 575 , 2434 (它承认所有实现使用控制块,似乎暗示多线程约束在某种程度上要求它),
Relevant DRs I found: 545, 575, 2434 (which acknowledge that all implementations are using a control block and seem to imply that multi-threading constraints somewhat mandate it), 2802 (which requires that the deleter only move constructible and thus prevents implementation where the deleter is copied between several shared_ptr
's).
这篇关于是否将shared_ptr的删除器存储在自定义分配器分配的内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!