0

The [[nodiscard]] attribute introduced in C++17 standard, and in case of the

... potentially-evaluated discarded-value expression,..., implementations are encouraged to issue a warning in such cases.

Source: n4659, C++17 final working draft.

Similar phrasing is used on cppreference, that in case of "violation":

the compiler is encouraged to issue a warning.

Why is the word encouraged used instead of required? Are there situations (except, the explicit cast to void) when a compiler is better off not issuing a warning? What is the reason behind softening the standard language in the particular case of relatively safe requirement to issue a warning no matter what (again, except, say, explicit cast to void)?

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
  • 2
    Are *any* warnings required in C++? – John Zwinck Aug 11 '19 at 02:18
  • @JohnZwinck very good point. That probably is the reason for the choice of the language in the standard related to encouraging\discouraging warnings. But now I obviously have a more general question. – Anton Menshov Aug 11 '19 at 02:23
  • 1
    One thing this does is allow implementations to ignore the attribute. The guideline the standards committee uses is that attributes should ignorable without changes to semantics (other than optimizations). – Justin Aug 11 '19 at 02:32

1 Answers1

3

The C++ standard specifies the behavior of a valid C++ program. In so doing, it also defines what "valid C++ program" means.

Diagnostics are only required for code which is ill-formed, code which is syntactically or semantically incorrect (and even then, there are some ill-formed circumstances that don't require diagnostics). Either the code is well-formed, or it is ill-formed and (usually) a diagnostic is displayed.

So the very idea of a "warning" is just not something the C++ standard recognizes, or is meant to recognize. Notice that even the "implementations are encouraged to issue a warning" statement is in a non-normative notation, rather than a legitimate specification of behavior.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982