60

Say I have

[[nodiscard]] int foo ()
{
    return 0;
}

int main ()
{
    foo ();
}

then

error: ignoring return value of ‘int foo()’, declared with attribute nodiscard [-Werror=unused-result]

but if

int x = foo ();

then

error: unused variable ‘x’ [-Werror=unused-variable]

Is there a clean way of telling the compiler "I want to discard this [[nodiscard]] value"?

3
  • 4
    Sorry if I'm nosy, but why do you want to discard the value? If it makes sense to run the code and discard the value, the function shouldn't be [[nodiscard]], right? I can think of up two reasons to call a [[nodiscard]]function for side effects, unit testing and cache warming. In the UT case, you normally want to compare the return value against some expected result, though.
    – Arne Vogel
    Commented Dec 3, 2018 at 10:14
  • 1
    In my particular case, a list container erase method returns the iterator to the subsequent list node, but that subsequent node iterator is already known to the calling function.
    – spraff
    Commented Dec 3, 2018 at 11:34
  • @spraff A function with side effects as you describe should probably not be marked [[nodiscard]]. Commented Sep 13, 2023 at 14:52

6 Answers 6

54

Cast it to void:

[[nodiscard]] int foo ()
{
    return 0;
}

int main ()
{
    static_cast<void>(foo());
}

This basically tells the compiler "Yes I know I'm discarding this, yes I'm sure of it."

2
  • 6
    If only it had been made mandatory in the language years, even decades, ago to have to cast-to-void ignored return values! Think how many bugs could have been averted in the edit-compile cycle before even getting to link/run/debug (much less production)!!
    – davidbak
    Commented Dec 2, 2018 at 16:11
  • 6
    @davidbak Except that there was no void in the original C language, and it is common practice to ignore the return value from some library functions (like printf). Adding a requirement to explicitly discard the return value from these functions would have broken almost every program in existence at the time. And made for some very inelegant looking code. Commented Dec 3, 2018 at 3:45
37

The WG14 nodiscard proposal discusses the rationale for allowing the diagnostic to be silenced by casting to void. It says casting to void is the encouraged (if non-normative) way to silence it which follows what the existing implementation do with __attribute__((warn_unused_result)):

The [[nodiscard]] attribute has extensive real-world use, being implemented by Clang and GCC as __attribute__((warn_unused_result)) , but was standardized under the name [[nodiscard]] by WG21. This proposal chose the identifier nodiscard because deviation from this name would create a needless incompatibility with C++.

The semantics of this attribute rely heavily on the notion of a use, the definition of which is left to implementation discretion. However, the non-normative guidance specified by WG21 is to encourage implementations to emit a warning diagnostic when a nodiscard function call is used in a potentially-evalulated discarded-value expression unless it is an explicit cast to void. This means that an implementation is not encouraged to perform dataflow analysis (like an initialized-but- unused local variable diagnostic would require). ...

The C++ way would be static_cast<void>.

See the draft C++ standard [[dcl.attr.nodiscard]p2:

[ Note: A nodiscard call is a function call expression that calls a function previously declared nodiscard, or whose return type is a possibly cv-qualified class or enumeration type marked nodiscard. Appearance of a nodiscard call as a potentially-evaluated discarded-value expression is discouraged unless explicitly cast to void. Implementations should issue a warning in such cases. This is typically because discarding the return value of a nodiscard call has surprising consequences. — end note]

This is a note, so non-normative but basically this is what existing implementations do with __attribute__((warn_unused_result)). Also, note a diagnostic for nodiscard is also also non-normative, so a diagnostic for violating nodiscard is not ill-formed but a quality of implementation just like suppressing via a cast to void is.

see the clang document on nodiscard, warn_unused_result:

Clang supports the ability to diagnose when the results of a function call expression are discarded under suspicious circumstances. A diagnostic is generated when a function or its return type is marked with [[nodiscard]] (or __attribute__((warn_unused_result))) and the function call appears as a potentially-evaluated discarded-value expression that is not explicitly cast to void.

0
28

CppCoreGuidelines suggest using std::ignore:

Never cast to (void) to ignore a [[nodiscard]] return value. If you deliberately want to discard such a result, first think hard about whether that is really a good idea (there is usually a good reason the author of the function or of the return type used [[nodiscard]] in the first place). If you still think it's appropriate and your code reviewer agrees, use std::ignore = to turn off the warning which is simple, portable, and easy to grep.

This is almost the same as boost::ignore_unused suggested in another answer, but out of std:: box.

However, there are drawbacks of using std::ignore:

  • Like other such helper function, it would instantiate, spending compile time, and be called (in non-optimized debug), spending run time
  • std::ignore is intended for another purpose, specifically, to discard a value assinged via std::tie1
  • std::ignore is not even guaranteed to suppress the warning

1 There's P2968R2 paper approved to C++26. It moves std::ingore out of <tuple> header, and specifies it in more details, so it enables other usages. Still, the paper does not endorse the use of std::ignore to suppress the ununsed parameter warning.

3
  • "std::ignore is not even guaranteed to suppress the warning" no, but insofar as it uses the result, it must suppress th warning that the result is unused... I get that this is not the original intended purpose of std::ignore, but it seems clearly required to work for it. Commented Jun 30, 2023 at 9:11
  • 1
    @underscore_d, yes, it works for usual compiler warnings which formally understands that the result is used by std::ignore, but it might be an issue for a static analysis tool which tries to track the control flow to some extent Commented Jun 30, 2023 at 9:50
  • The only correct answer!
    – olegrog
    Commented Sep 20, 2023 at 18:34
22

You can also tag the returned int with another tag:

[[nodiscard]] int foo ()
{
    return 0;
}

int main ()
{
    [[maybe_unused]] int i = foo ();
}

Could be useful if you have some debug-only code that requires the value.

4
  • I just turn off the unused-variable warning with a pragma, globally. There is no more useless warning from compilers than that one. Oh wait, yes there is: Unused parameter (with the implied suggestion that you remove the parameter's identifier from the signature)!
    – davidbak
    Commented Dec 2, 2018 at 16:17
  • 8
    Actually, I don't agree. I turned the warning on on purpose on my app, because I don't want to have clutter everywhere, except maybe when I know that with this attribute. Different policies, different approaches ;) Commented Dec 2, 2018 at 16:19
  • 2
    I liked this solution better than casting to void which I find a bit unintuitive. I also don't find the unused variable/parameter to be useless, in fact, it sometimes catches a bug in a large legacy code base where functions are in the thousands of lines (yeah I know x-). Just my 2c.
    – AndersK
    Commented Dec 2, 2018 at 16:23
  • The reason for my downvote is, that you actually bind the return value of foo and not discard it. The difference is in the drop order or that variable. If you bind it, the value will be dropped after the variable goes out of scope, if you discard it, the value will be dropped immediately.
    – hellow
    Commented May 4, 2023 at 6:36
12

I use a (empty) helper function "discard"

template<typename T>
void discard(const T&) {}

[[nodiscard]] int foo ()
{
    return 0;
}

int main ()
{
    discard(foo());
}

to intentionally discard a [[nodiscard]] value.

2
  • this only works if you don't have warning for unused function parameters enabled Commented Aug 23, 2023 at 13:51
  • @BalázsKovacsics In which compiler? Usually when you don't name a function parameter, warning like -Wunused-parameter won't be shown
    – MatG
    Commented Dec 12, 2023 at 11:00
3

With Boost:

#include <boost/core/ignore_unused.hpp>
int main ()
{
    boost::ignore_unused(foo ());
}

boost::ignore_unused takes its parameter(s) by reference to const, so the argument must be something you can bind to a reference to const. I'm fairly sure that anything that can be a function return type (other than void, of course!) should be OK here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.