jeudi 2 mars 2017

How can I make a valid C++ alias template for std::unique_ptr

I want to make an alias template for unique_ptr that supplies my own deleter function.

unique_ptr has both a scalar and an array implementation, they're defined like this:

template <class T, class D = default_delete<T>>
class unique_ptr // scalar

template <class T, class D>
class unique_ptr<T[], D> // array

I'm running into trouble trying to override both the scalar and array versions of unique_ptr. It's easy to make an alias for just one version, like this:

template<class T>
struct Deleter
{
    public:

    void operator()(T* ptr)
    {
        delete ptr;
    }
};

template<class T>
using my_unique_ptr = std::unique_ptr<T Deleter<T>>;

But when I try to add a second alias, like this:

template<class T>
using my_unique_ptr = std::unique_ptr<T[], ArrayDeleter<T>>;

... I end up with compiler errors because "my_unique_ptr" is ambiguous.

My question is: How can I create a single aliased name that works for both the array and scalar versions of unique_ptr?

Aucun commentaire:

Enregistrer un commentaire