我有一些代码,我正在改装以直接使用 anallocator
而不是。这段代码的公共接口的一部分不是返回一个 bald 指针,而是一个(由 bald 指针从.operator new
operator delete
shared_ptr
new
我在分配器的公共合同中看到了几个名为pointer
,const_reference
等的 typedef。这个想法不是T *
直接引用类型,我可能会使用pointer
,允许分配器作者插入 C 指针以外的东西。
但是我反过来将普通指针包装在一个智能指针中,它需要一个诚实的 C 指针(我认为。)如果我尝试将我的代码更改为使用分配器的 typedef,我会遇到麻烦吗?我还没有尝试过(因为有一些腿部工作要做只是为了尝试)所以我先问..
编辑:这是我想要更改的一些代码(我第一次没有包括,因为它不是很友好。)
template<typename Injector, typename Iface, typename Allocator, typename Impl, typename A1>
class New<Injector, Iface, Allocator, Impl, Impl(A1)>: public Binding<Injector> {
public:
static Impl * provide(Injector const & injector) {
SAUCE_SHARED_PTR<A1> a1(injector.template provide<A1>());
return initializer(injector).template construct<Impl, SAUCE_SHARED_PTR<A1> >(a1);
}
static void dispose(Injector const & injector, Iface * iface) {
Impl * impl = static_cast<Impl *>(iface);
initializer(injector).destroy(impl);
}
};
// Elsewhere
template<typename Iface>
SAUCE_SHARED_PTR<Iface> provide() const {
Iface * provided = provideFromBinding<Iface>(
Module::template bindings<Injector_> );
SAUCE_SHARED_PTR<Iface> smartPointer;
smartPointer.reset(provided, deleter);
return smartPointer;
}
特别是,在表达式的结果上调用的construct
and模板方法当前嵌入了直接使用and 。我希望它改为使用,并将它们切换到放置新/显式销毁,以制作类似destroy
initializer(injector)
new
delete
Allocator
template<typename Injector, typename Iface, typename Allocator, typename Impl, typename A1>
class New<Injector, Iface, Allocator, Impl, Impl(A1)>: public Binding<Injector> {
public:
static Impl * provide(Injector const & injector) {
SAUCE_SHARED_PTR<A1> a1(injector.template provide<A1>());
Allocator allocator;
Impl * impl = allocator.allocate(sizeof(Impl));
initializer(injector).template construct<Impl, SAUCE_SHARED_PTR<A1> >(impl, a1);
return impl;
}
static void dispose(Injector const & injector, Iface * iface) {
Allocator allocator;
Impl * impl = static_cast<Impl *>(iface);
initializer(injector).destroy(impl);
allocator.deallocate(impl, sizeof(Impl));
}
};
问题是,我应该尝试尊重分配器的 typedef 吗?看起来我应该是,否则我没有正确使用分配器(没关系,从务实的角度讲,几乎所有这些都将具有“无聊”的 typedef 值。)
我当然可以让它返回Allocator::pointer
而不是返回Impl *
,直到我尝试将它推入shared_ptr
. 也许它也在那里工作?那是我的问题。也许比我更熟悉标准的人会说“只要Allocator::pointer
像 a 那样的庸医operator*
并且你注册了一个自定义删除器,你就可以了。”
编辑: @bdonlan 提出了一个很好的观点,即 ashared_ptr
是以什么为模板Allocator::value_type
,而不是什么Allocator::pointer
。相反,它可能在内部持有一个Allocator::value_type *
。这似乎回答了这个问题,除了分配器作者可能选择以这样一种方式实现operator Allocator::value_type *
它们Allocator::pointer
,即使使用转换后的值,它也根据一些具体陈述的语义“一切正常”。
标准是否要求分配器作者这样做?
编辑:见相关。