Domanda

shared_ptr is defined as

template< class T > class shared_ptr;

the custom allocator is passed as constructor argument

template< class Y, class Deleter, class Alloc >
shared_ptr( Y* ptr, Deleter d, Alloc alloc );

Why there is no

template< class T, class Alloc = std::allocator<T> > class shared_ptr;

?

I have the feeling that this make life easier in certain cases, but on the other end it prevents doing something like:

namespace App{
    template <typename T>
    using Shared = std::shared_ptr<T,myAllocator<T>>; //pool allocator for control blocks
}

Also make_shared does not allows that because it already takes as arguments the constructor's arguments.

Any Ideas?

È stato utile?

Soluzione

It wouldn't make life easier as it would artifically constrict any client of shared_ptr to either be tied to one particular allocator type or itself by templated on an allocator type.

Only some constructors and functions (e.g. some reset overloads) need to know about the allocator used to allocated the shared object. Many clients can happily be allocator agnostic.

Having fewer template parameters on the type is less constraining. Note that template parameters on class types must usually be specified explicitly where as on function templates they can often be deduced from parameters.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top