[[nodiscard]] and KDE coding policy

Ivan Čukić ivan.cukic at kde.org
Fri Jul 31 14:25:23 BST 2026


> > > Yeah, I don't think getters need `[[nodiscard]]`, for the same reason
> > > that
> > > `std::min` and `std::move` don't have it:

Do not assume that because most std:: functions don't have it, that
users should be discouraged to use it.

There is a nice comment by Jonathan Wakely (ISO C++ committee member,
maintainer of libstdc++) of the current status of [[nodiscard]] in the 
standard:

https://github.com/isocpp/CppCoreGuidelines/issues/2109

--
That's not because the standard thinks it shouldn't be used elsewhere. There 
have been several proposals to add it to loads of functions in the standard 
library, but I (and others) argued strongly against that, because:

- Specifying it in the standard has no normative effect, implementations are 
allowed to ignore the attribute, and even if they don't ignore it, they're not 
required to issue a diagnostic.
- Specifying it in a few places in the standard could be interpreted as saying 
it shouldn't be used elsewhere. That could result in users reporting bogus 
bugs about nodiscard warnings from other functions.
- Specifying it in the standard would cause churn for implementation vendors 
who do want to add it where the standard "requires", to avoid complaints about 
being non-conforming.
- Reviewing and approving those proposals would have eaten up hundreds of 
person hours in the committee, which should be spent on far more productive 
work.
- It can (and IMHO should) be left to implementation vendors to just Do The 
Right Thing.

And the Right Thing is to use it widely, everywhere that it makes sense.

IMHO compilers should warn about discarded values for all comparison 
operators, all begin and end accessors, nearly all const member functions, 
etc. etc. because the chances that a == b; as an entire statement is a typo 
for = is much higher than the chances that the developer really wanted to 
compare two things just for the thrill of it. Compilers already warn about 
this for fundamental types, so it makes sense to do so for strings, containers 
etc. too. But unfortunately they're not smart enough to do it automatically, 
so it is useful to add [[nodiscard]] to them.* And std::lib implementations do 
that, without needing the standard to tell them to.

tl;dr do not assume from the absence of [[nodiscard]] attributes in the 
standard specification that the committee thinks it should be used sparingly. 
The opposite is true.
--

Cheers,
Ivan

-- 
Dr Ivan Čukić
ivan.cukic at kde.org, https://cukic.co/
gpg key fingerprint: 8FE4 D32F 7061 EA9C 8232 07AE 01C6 CE2B FF04 1C12




More information about the kde-devel mailing list