Is nodiscard necessary on operators?

3.7k views Asked by At

Is the [[nodiscard]] attribute necessary on operators? Or is it safe to assume the compiler will emit a warning like it does for most suspiciously discarded things?

E.g. an overloaded operator+, should one apply the attribute? What about special operators like function-cast operators or new operators? When is it pedantic?

3

There are 3 answers

1
Evg On BEST ANSWER

Let me cite the following paper by N.Josuttis: "[[nodiscard]] in the library" (with some omissions, see the full paper):

C++17 introduced the [[nodiscard]] attribute. The question is, where to apply it now in the standard library. It should be added where:

  • not using the return value always is a “huge mistake” (e.g. always resulting in resource leak),
  • not using the return value is a source of trouble and easily can happen (not obvious that something is wrong).

It should not be added when:

  • not using the return value is a possible/common way of programming at least for some input,
  • not using the return value makes no sense but doesn’t hurt and is usually not an error.

So, [[nodiscard]] should not signal bad code if this

  • can be useful not to use the return value,
  • is common not to use the return value,
  • doesn’t hurt and probably no state change was meant that doesn’t happen.
4
463035818_is_not_an_ai On

It is never necessary to add the [[nodiscard]] attribute. From cppreference:

If a function declared nodiscard or a function returning an enumeration or class declared nodiscard by value is called from a discarded-value expression other than a cast to void, the compiler is encouraged to issue a warning.

Note the last part: "... the compiler is encouraged to issue a warning." The is no guarantee, as far as the standard is concerned, that there actually will be a warning. Its a quality of implementation issue. If your compiler does emit a warning (read the docs) and if you are treating such warnings as errors, then the [[nodiscard]] can be of great use.

It is pedantic to use the attribute on operators where discarding the return is only potentially an error. I would only use it when calling the operator and discarding the result is always a logic error. Many operators use the return value merely to enable chaining and the [[nodiscard]] would rather be an annoyance on such operators. There are cases where the decision is not so obvious and it is a matter of opinion and style what you choose.

0
eerorika On

Is nodiscard necessary on operators?

No. nodiscard and other attribures are optional.

Or is it safe to assume the compiler will emit a warning like it does for most suspiciously discarded things?

There is no guarantee about any warning in the language except when the program is ill formed.

I would also not assume warning without nodiscard because there are many cases where result of operation is intentionally discarded. A common example:

a = b;  // result of assignment was discarded

In fact, if all discarded results resulted in a warning, then there would not be any purpose for the nodiscard attribure.