https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36587

--- Comment #16 from Kaz Kylheku <kkylheku at gmail dot com> ---
(In reply to Andrew Pinski from comment #14)
> C++17 adds nodiscard attribute which can be placed on class/struct types,
> functions, constructors and others and then you get a warning if you ignore
> the value. In the case of struct/class types and constructors that will warn
> when a temporary value is ignored. Exactly in the case you were asking for a
> warning.
> 
> Which was added to GCC by r7-377-gb632761d2c6a65 (and fixes afterwards).
> 
> So closing as fixed.

I'm afraid that this doesn't seem like a good resolution. The feature you are
referring to is opt-in, per class, whereas the proposed warning imposes the
behavior for every class.

This is a big difference.

The number of situations in which "classname(arg);" as an expression statement
with a discarded value is correct is pretty small. This is almost always going
to be a coding mistake. Where it isn't a coding mistake, the intent can be
indicated using "(void) classname(arg);".

The good news is that, at least it would seem that the implementation of the
warning can be piggybacked on the nodiscard attribute implementation. The
simplest possible requirement is that the option makes the compiler pretend
that the attribute has been asserted for every class. (I say tentatively,
without having studied the attribute's semantics in detail.)

nodiscard could be "stronger" in that if it is asserted, then even the cast to
(void) won't silence the diagnostic, so that it's still meaningful to use in
spite of the warning option.

Reply via email to