[Development] The future of smart pointers in Qt API

Ville Voutilainen ville.voutilainen at gmail.com
Tue Feb 4 16:12:44 CET 2020


On Tue, 4 Feb 2020 at 15:40, Volker Hilsheimer <volker.hilsheimer at qt.io> wrote:
> >> This works, but now you are encoding the visual hierarchy of the widgets in two places: when asking the presumed parent to create the widgets; and when setting up the layout, which might do things differently (esp once you start refactoring complex UIs).#
> >
> > That's not a problem. The code simply behaves as if you would have used the current constructors that take a parent parameter.
> >
> > Ownership transfers between different qt parents don't violate the invariant. And they continue to work just like before.
>
>
> So how is the above usage of makeChild an improvement over calling operator new to instantiate your objects?

The result of makeChild never leaks.

> > But that just means that QBoxLayout::addWidget(std::unique_ptr<>) has to behave imho saner in that edge case and actually delete the widgets that were added but never parented. (And I should really implement that to show how it would work.)
>
>
> That seems like a terrible idea. One of the layout’s jobs is to set up the parent/child hierarchy so that you as a developer don’t have to.

I don't see how that idea changes that.

> Do we really want to make it harder for people to write UIs just because the resulting code doesn’t satisfy a C++ idiom?

It's not a question of satisfying idioms for the sake of satisfying
idioms, it's a question of avoiding bugs that we know
based on decades of experience to be difficult to avoid with raw pointers.

> If you don’t care about the things that QObject/QWidget does for you, then don’t use QObject/QWidget. And if you do use QObject/QWidget and get a consistent object model from it, then I’m really not convinced that adding smart pointers to the API is making the code clearer, or in any way more explicit. It’s not like the ability of using std::unique_ptr means that people no longer have to be familiar with Qt’s basic concepts to use it correctly.

Correct, but if code moves a std::unique_ptr into a function call
argument, I know that ownership is transferred, by just glancing at
the code. With a raw pointer, I don't know that.


More information about the Development mailing list