[Development] Deleter that calls deleteLater() for use with std::unique_ptr<> and other smart pointers

Konstantin Tokarev annulen at yandex.ru
Wed Apr 19 20:44:44 CEST 2017



19.04.2017, 20:58, "Ville Voutilainen" <ville.voutilainen at gmail.com>:
> On 19 April 2017 at 20:48, Konstantin Tokarev <annulen at yandex.ru> wrote:
>>  19.04.2017, 20:43, "Thiago Macieira" <thiago.macieira at intel.com>:
>>>  On quarta-feira, 19 de abril de 2017 06:44:24 PDT Konstantin Tokarev wrote:
>>>>   Hello,
>>>>
>>>>   I think it will be a good idea to provide implementation of functor calling
>>>>   deleteLater() instead of delete which can be used with std::unique_ptr,
>>>>   std::shared_ptr and other smart pointers with compatible interface.
>>>>
>>>>   We already have QScopedPointerObjectDeleteLater, however it implements
>>>>   cleanup(T*) instead of operator()(T*).
>>>>
>>>>   Any suggestions how to name this new class and where to place it?
>>>
>>>  QSharedPointer already supports a PMF as a deleter.
>>>
>>>          auto ptr = QSharedPointer<QObject *>(new QObject, &QObject::deleteLater);
>>>
>>>  So I think this actually belongs in the std-proposals mailing list, not in Qt.
>>>
>>>  Until C++20 gets it, you can use a lambda.
>>
>>  It's not possible to have members with auto-deduced types in non-template class,
>>  so I actually cannot.
>
> I fail to see what auto-deduced members have to do with it:
>
> std::unique_ptr<QObject, void(*)(QObject*)> upqobj{nullptr,
> [](QObject* obj) { obj->deleteLater(); }};

Thanks, it works.

Except it's totally unusable because of bolerplate spreading.
Compare with

std::unique_ptr<QObject, QObject::DeleteLater>

that you can initialize without any boilerplate. Also, the second version
does not increase size of unique_ptr with size of function pointer.

-- 
Regards,
Konstantin



More information about the Development mailing list