[Development] unique_ptr and Qt, Take 2

Matthew Woehlke mwoehlke.floss at gmail.com
Thu Jun 13 17:25:10 CEST 2019


On 13/06/2019 03.03, Marco Bubke wrote:
> On June 12, 2019 23:13:14 Matthew Woehlke <mwoehlke.floss at gmail.com> wrote:
>>> me here. I think Daniel is on to something and we really should explore
>>> having smart pointers in Qt 6, but at this point I'm not convinced
>>> std::unique_ptr alone is it. I'd like to see what we could achieve with a
>>> QObject-specific smart pointer that understood ownership-taking functions
>>> (setParent, addWidget, etc.) and observing-only API (like connect()).
>>
>> Idea: make this hypothetical pointer be a QObject that initially "owns"
>> the object to which it points. If the object is reparented, the pointer
>> will stop "owning" it but will still have the reference.
>
> Can you implement it so that it fits owner<T>? One of the purposes is that the static analysis can be sure about the ownership.

I don't think so; at least, not without a lot of associated work. Part
of the above idea is that I believe it is notionally implementable *now*
(or even as far back as Qt4 at least). You can safely pass the raw
pointer to methods that may or may not reparent the object, and
everything will continue to work, but currently it isn't obvious what
methods will or won't take ownership, and in some cases the answer to
that question depends on additional (run-time) information. (Note in
particular the QLayout example as mentioned elsewhere in this thread.)

-- 
Matthew


More information about the Development mailing list