Is it possible to ignore [[nodiscard]] in a special case?

Viewed 3027

C++17 has a new attribute, [[nodiscard]].

Suppose, that I have a Result struct, which has this attribute:

struct [[nodiscard]] Result {
};

Now, if I call a function which returns Result, I got a warning if I don't check the returned Result:

Result someFunction();

int main() {
    someFunction(); // warning here, as I don't check someFunction's return value
}

This program generates:

warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]

So far, so good. Now suppose, that I have a special function, for which I still want to return Result, but I don't want this warning generated, if the check is omitted:

Result someNonCriticalFunction();

int main() {
    someNonCriticalFunction(); // I don't want to generate a warning here
}

It is because, someNonCriticalFunction() does something non-critical (for example, something like printf - I bet that no-one checks printf's return value all the time); most cases, I don't care if it fails. But I still want it to return Result, as in some rare cases, I do need its Result.

Is it possible to do this somehow?


Possible solutions which I don't like:

  • I would not like calling it as (void)someNonCriticalFunction(), because this function is called a lot of times, it is awkward
  • creating a wrapper around someNonCriticalFunction(), which calls (void)someNonCriticalFunction(): I don't want to have a differently named function just because of this
  • removing [[nodiscard]] from Result, and add it to every function which returns Result
4 Answers

They say that every problem in computer science can be solved by adding another layer of indirection:

template <bool nodiscard=true>
struct Result;

template <>
struct Result<false> {
    // the actual implementation
};

template <>
struct [[nodiscard]] Result<true>
    : Result<false>
{
    using Result<false>::Result;
};

This is effectively making Result conditionally [[nodiscard]], which allows:

Result<true> someFunction();
Result<false> someNonCriticalFunction();

int main() {
    someFunction();            // warning here
    someNonCriticalFunction(); // no warning here
}

Although really, this is identical to:

  • removing [[nodiscard]] from Result, and add it to every function which returns Result

which gets my vote to begin with.

I recommend the option you ruled out:

"removing [[nodiscard]] from Result, and add it to every function which returns Result."

But since you don't seem happy with it, here's another solution, using bog-standard inheritance:

struct [[nodiscard]] Result {
};

struct DiscardableResult: public Result {
};

For the functions where you can discard the result, use DiscardableResult as return type:

Result func1();
DiscardableResult func2();

func1(); // will warn
func2(); // will not warn

cast the result to a (void *).

int main()
{
    (void *)someFunction(); //Warning will be gone.
}

This way you "used" your result as far as the compiler is concerned. Great for when you are using a library where nodiscard has been used and you really don't care to know the result.

Related