[Development] The future of smart pointers in Qt API

Vitaly Fanaskov vitaly.fanaskov at qt.io
Tue Feb 11 16:15:11 CET 2020


I want to summarize intermediate results of the discussion and return it 
back to the track.


Subject: using smart pointers in the API.
Good idea. Better to use than not because of automatic lifetime 
management, explicit ownership semantics, and code base maintainability 
is simpler. However, it doesn’t mean that this is a mandatory choice for 
all API, just a recommended way so far.


Subject: standard smart pointers vs. Qtish wrappers.
In general, people want to use standard smart pointers. There are a few 
main reasons: std-things are already in Qt API, this is a part of C++ 
language standard, it requires almost no maintainability, people outside 
of Qt ecosystem most likely get used to standard smart pointers for nine 
years.
We should also move Qt smart pointers to Qt5Compat module. The destiny 
of QPointer is not well defined so far.


Subject: raw pointers for passing mandatory parameters vs. using 
references.
Allow both approaches, recommend using references (and/or smart 
pointers) when acceptable. Not too many arguments collected here, just 
try to make Qt API more modern.


There are a few irrelevant discussions. Start a new thread if you want 
to continue discussing them, please.


Irrelevant subject: smart pointers vs. parent-child lifetime management 
model.
It’s too fragile to touch in Qt6. Adding smart pointers around is 
questionable solution, it’s unclear whether it worth doing or not. 
Having two different but coupled mechanisms for lifetime management is 
not such a good idea.

Irrelevant subject: something about Qt containers.

On 1/31/20 11:07 AM, Vitaly Fanaskov wrote:
> Hello everyone,
>
> We’ve been discussing for a while how Qt6 API can be improved with using
> smart pointers. Recently we came into some conclusions and want to
> discuss them with the community.
>
> Smart pointers are for sure much better to use than raw pointers for
> many reasons. They manage lifetime automatically, show ownership
> semantic, and make code safer. It’s planned to officially recommend them
> to use in Qt6 API instead of raw pointers whenever this is possible. For
> sure, it should only be a choice for newly designed API.
>
> But how to use them in the API and which way is preferable is still
> unclear. There are two main options we have:
>
> 1) Use std::*  smart pointers as-is.
>
> 2) Add Qt-style wrappers around std::* smart pointers and move old
> implementations of Qt smart pointers to the Qt5Compact module.
>
> Both options have pros and cons. It would be useful to hear your
> thoughts on it. It’s worth mentioning that some other options, like
> using Qt smart pointers as-is, were also discussed. They were found less
> suitable, but feel free to share your opinion if you disagree.
>
> Another thing to discuss is whether we should use raw pointers in the
> API at all or not. There are a few options again:
>
> 1) Yes
>
> 2) No. Use “modern” approaches instead (pass mandatory dependencies by
> either reference or const reference when using smart pointers makes no
> sense, use something optional-like or tuples to return extra data from
> functions, and so on)
>
> 3) Mix 1 and 2.
>
> There are pros and cons for all options. Share your opinion once again,
> please.
>
> If there are any related things to discuss, let’s do that in this thread.
>
-- 
Best Regards,

Fanaskov Vitaly
Senior Software Engineer

The Qt Company / Qt Quick and Widgets Team



More information about the Development mailing list