Hoping that EWGI will reconvene in Kona, I have prepared a proposal to
allow attributes on expressions, and also proposes the [[discard]]
attribute. It is a draft ... of a draft -- there's still some design
work to be done, possibly with EWG(I) help.
https://isocpp.org/files/papers/D2992R0.html
As usual, any early feedback is very much appreciated.
Copying my comments from Slack...
> We can’t disallow it, in the spirit of ignorability of attributes.
seems wrong; adding an attribute in an inappropriate location (e.g. [[fallthrough]] on a non-null statement, or on a function declaration) is certainly permitted to (and does) give a hard error.
> Appearance of a discouraged nodiscard call is discouraged.
:perfection:
I suggest combining this sentence with the next one: "Implementations should issue a warning whenever a discouraged nodiscard call is encountered."
> explicitly cast to void
I can see this example of yours causing headaches for style-guide writers:
> enable_missile_safety_mode(); // warning encouraged
> [[discard]] my_unique(), enable_missile_safety_mode(); // warning not encouraged
The benefit of (void)my_unique() is that the cast binds tightly to the ignored thing; [[discard]] as proposed seems to bind super loosely. If it bound as tight as any other prefix operator, that would help.(As you said on Slack, the counterargument is that people who use [[discard]] should not be using the comma operator.)
It looks to me like there are only three kinds of discarded-value expressions in the entire language:
FWIW, personally I'd be in F of allowing attributes on expressions (with prefix syntax and prefix-operator high precedence, which at the moment you have not got), but A or SA the proposed [[discard]] attribute since it's just another way of spelling something that — as you point out — we've already got several competing ways of spelling. And it's much more cumbersome to write, to read, and probably even to parse, than (void)f().