dimanche 1 janvier 2017

std::unique_ptr, custom deleter and type change

I would like to use unique_ptr with my deleter. I would like my unique_ptr with my deleter to be fully compatible with unique_ptr with default deleter.

I did so:

template <typename T>
struct QObjectDeleteLaterDeletor :
        public std::default_delete<T>
{
    void operator()(T *p)
    {
        p->deleteLater();
    }
};

template <typename T, class... Args>
std::unique_ptr<T> qtMakeUniqueSpecial(Args&&... args)
{
    return std::unique_ptr<T>(
                new T(std::forward<Args>(args)...),
                QObjectDeleteLaterDeletor<T>());
}

This compiles, but does not work. My custom deleter ignored and default one used instead as if I did not specify it at all.

I need all of this to be possible to do things like that:

auto ptr1 = qtMakeUniqueSpecial<MyObject>();
std::unique_ptr<MyObject> ptr2;
ptr2 = std::move(ptr1);

Please note that now even ptr1.reset() will lead to calling the standard deleter, not my one.

Is it even possible?

Aucun commentaire:

Enregistrer un commentaire