Re: [[nodiscard]] and KDE coding policy
Ivan Čukić <[email protected]> Fri, 31 Jul 2026 15:25:23 +0200
| Newsgroups | gmane.comp.kde.devel.general |
|---|---|
| Message-ID | <[email protected]> |
> > > 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=20 standard: https://github.com/isocpp/CppCoreGuidelines/issues/2109 =2D- That's not because the standard thinks it shouldn't be used elsewhere. Ther= e=20 have been several proposals to add it to loads of functions in the standard= =20 library, but I (and others) argued strongly against that, because: =2D Specifying it in the standard has no normative effect, implementations = are=20 allowed to ignore the attribute, and even if they don't ignore it, they're = not=20 required to issue a diagnostic. =2D Specifying it in a few places in the standard could be interpreted as s= aying=20 it shouldn't be used elsewhere. That could result in users reporting bogus= =20 bugs about nodiscard warnings from other functions. =2D Specifying it in the standard would cause churn for implementation vend= ors=20 who do want to add it where the standard "requires", to avoid complaints ab= out=20 being non-conforming. =2D Reviewing and approving those proposals would have eaten up hundreds of= =20 person hours in the committee, which should be spent on far more productive= =20 work. =2D It can (and IMHO should) be left to implementation vendors to just Do T= he=20 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=20 operators, all begin and end accessors, nearly all const member functions,= =20 etc. etc. because the chances that a =3D=3D b; as an entire statement is a = typo=20 for =3D is much higher than the chances that the developer really wanted to= =20 compare two things just for the thrill of it. Compilers already warn about= =20 this for fundamental types, so it makes sense to do so for strings, contain= ers=20 etc. too. But unfortunately they're not smart enough to do it automatically= ,=20 so it is useful to add [[nodiscard]] to them.* And std::lib implementations= do=20 that, without needing the standard to tell them to. tl;dr do not assume from the absence of [[nodiscard]] attributes in the=20 standard specification that the committee thinks it should be used sparingl= y.=20 The opposite is true. =2D- Cheers, Ivan =2D-=20 Dr Ivan =C4=8Cuki=C4=87 [email protected], https://cukic.co/ gpg key fingerprint: 8FE4 D32F 7061 EA9C 8232 07AE 01C6 CE2B FF04 1C12