C++ Logo


Advanced search

Re: [std-proposals] [[nodiscard]] friend

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 18 Jan 2023 13:33:11 -0500
On Wed, Jan 18, 2023 at 12:36 PM Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> On Wed, Jan 18, 2023 at 11:51 AM Robert Allan Schwartz via Std-Proposals <std-proposals_at_[hidden]> wrote:
>> Header <stop_token>:
>> [[nodiscard]] friend bool operator==(const stop_token& lhs, const stop_token& rhs) noexcept;
>> I would have thought that this should be:
>> friend [[nodiscard]] bool operator==(const stop_token& lhs, const stop_token& rhs) noexcept;
>> i.e. the attribute pertains to the function, not to the friend declaration,
>> but I was told that an attribute cannot appear in this position,
>> hence it must be as it is in <stop_token>.
>> Perhaps the grammar should be amended to allow an attribute in this position?
> Hi Robert,
> (Unlike your other thread, this one is a feature request / behavioral change, and thus would have to be proposed as a paper and discussed in committee; even if you had wording for it, it should never be submitted as a pull request. Just so we're clear on that. :))
> This proposal lacks motivation. You're asking to complicate the grammar, right? This will create burdens for the committee/specifiers, for compiler vendors/implementors, for users who now have to deal with yet another little difference between C++2b and C++2c. Those are all costs. And the benefit is... what? That you now get a stylistic choice of two different places it's legal to put attributes like `[[nodiscard]]`?
> Alternatively, just don't do any of that; and then the only cost is to (an ever-decreasing number of) programmers who would stylistically prefer to write `friend [[nodiscard]] bool` but are forced by the simple grammar of C++ to write `[[nodiscard]] friend bool`. That's a much smaller cost than all the costs above; and it has all the same benefits plus two — compiler enforcement of the preferred style (the alternative style is rejected by the compiler, hooray!), and its already-being-implemented-as-the-status-quo (so vendors don't need to do any work).

I'm not sure it's reasonable to call anything around the C++ grammar
rules for function declarations or attributes "simple grammar". Look
at it conceptually: what does [[nodiscard]] *mean*?

It describes how the return value of the function has to be used.
Conceptually it is a modifier of the return type, so it feels a lot
more like `const`. And you don't write it `const friend bool ...`. The
`const` applies to the return type, so it should be on the return

Just like `[[nodiscard]]`. Again, conceptually.

This is probably a good example of *why* doing this sort of stuff
through attributes rather than actual syntax is a problem.

Received on 2023-01-18 18:33:32