[Development] C++20 ctor-level [[nodiscard]] (was: Re: C++20 @ Qt)

Marc Mutz marc.mutz at qt.io
Fri Jun 16 08:49:49 CEST 2023


On 14.06.23 08:33, Marc Mutz via Development wrote:

Two more data points:

It doesn't matter whether I write Q_FOO_EXPORT [[nodiscard]] or
[[nodicard]] Q_FOO_EXPORT, I always get an error on some compiler.

So compilers don't like class-level [[nodiscard]] in conjunction with an 
export macro. This is fodder for both my claims that a) non-polymophic 
classes should not be exported wholesale and b) class-level 
[[nodiscard]] is not a good idea.

In fact, I have yet to see a convincing user-case for class-level 
[[nodiscard]], apart from the usual error_code example.

P1771 suggests to mark only those ctors [[nodiscard]] that allocate 
resources (in the widest possible sense). That is a narrower rule that 
"all ctors". Depends a bit on how compilers interpret the semantics. We 
have one CI log¹ where GCC 10.3 apparently warns about a discarded value in

    QDebugSaver saver(d);

That would be a fatal false-positive. The same compiler seems to be 
warning about QScopedPointer

    QScopedPointer ptr(QThread::create(~~~));
    ~~~use ptr~~~

and I have not been able to reproduce the issue with my GCC 10.3 
(but I have a different Linux distribution version).

¹ 
https://testresults.qt.io/logs/qt/qtbase/c35764f80a73271dc0a795260652a8405def6e88/LinuxUbuntu_22_04x86_64LinuxQEMUarmv7GCCqtci-linux-Ubuntu-22.04-x86_64-51-0f3b05Sccache_UseConfigure/3389fd97c7323d93c525d93a93b1346f06b53d0a/build_1691805008/log.txt.gz

I'll be holding my patches in topic:nodiscard-ctor until we have come up 
with a set of rules for [[nodiscard]].

Thanks,
Marc

-- 
Marc Mutz <marc.mutz at qt.io>
Principal Software Engineer

The Qt Company
Erich-Thilo-Str. 10 12489
Berlin, Germany
www.qt.io

Geschäftsführer: Mika Pälsi, Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht Charlottenburg,
HRB 144331 B



More information about the Development mailing list