allocator代わりにoperator new直接operator delete使用するように改造しているコードがいくつかあります。このコードのパブリックインターフェイスの一部は、ハゲポインターではなく、shared_ptr(ハゲポインターから構築されて)を返すことですnew。
pointerアロケータの公開契約には、、などの名前のいくつかのtypedefがありますconst_reference。アイデアは、型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;
}
特に、式の結果で呼び出されるconstructand destroytemplateメソッドは、現在、とinitializer(injector)の直接使用を埋め込んでいます。代わりに使用して、新しい/明示的な破棄の配置に切り替えて、次のようなものを作成したいと思います。newdeleteAllocator
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_ptrAllocator::pointeroperator*
編集: @bdonlanは、aがではなく、でshared_ptrあるものにテンプレート化されているという優れた点を提起します。代わりに、おそらく内部的に保持されます。これは、アロケータの作成者が、変換された値を使用する場合でも、具体的に述べられたセマンティクスに従って「すべてうまくいく」ように実装することを選択する可能性があることを除いて、質問に答えるようです。Allocator::value_typeAllocator::pointerAllocator::value_type *operator Allocator::value_type *Allocator::pointer
標準では、アロケータの作成者がこれを行う必要がありますか?
編集:関連を参照してください。