143
int fn();

void whatever()
{
    (void) fn();
}

Is there any reason for casting an unused return value to void, or am I right in thinking it's a complete waste of time?

10 Answers 10

96

David's answer pretty much covers the motivation for this, to explicitly show other "developers" that you know this function returns but you're explicitly ignoring it.

This is a way to ensure that where necessary error codes are always handled.

I think for C++ this is probably the only place that I prefer to use C-style casts too, since using the full static cast notation just feels like overkill here. Finally, if you're reviewing a coding standard or writing one, then it's also a good idea to explicitly state that calls to overloaded operators (not using function call notation) should be exempt from this too:

class A {};
A operator+(A const &, A const &);

int main () {
  A a;
  a + a;                 // Not a problem
  (void)operator+(a,a);  // Using function call notation - so add the cast.
3
  • this is the only place i prefer c-style cast too. tho in my coding standard, i would add (void) to "a + a;" too (properly parened, of course :p) Mar 27, 2009 at 13:21
  • 10
    Somewhat off topic, but why ever would you do "a + a;" like that without using the return value? That really seems like an abuse of side effects to me, and obfuscates the intent of the code.
    – Rob K
    Mar 27, 2009 at 16:38
  • 5
    Well, the one that you'll use every day will be 'a = a'. This returns the assigned to object (for all well behaving classes that is! :)). Another example is: os <<"Hello World" << std::endl. Each of them returns the "os" object. Mar 27, 2009 at 18:07
55

At work we use that to acknowledge that the function has a return value but the developer has asserted that it is safe to ignore it. Since you tagged the question as C++ you should be using static_cast:

static_cast<void>(fn());

As far as the compiler goes casting the return value to void has little meaning.

7
  • Does it suppress the warning about unused return values? Mar 27, 2009 at 13:07
  • No, it does not. @Mykola: With GCC4 you can attach an attribute that says that the return value shouldn't be ignored which will trigger a warning.
    – David Holm
    Mar 27, 2009 at 13:08
  • 4
    I use g++ and it gives warning even with such cast.
    – klew
    Mar 27, 2009 at 13:08
  • 2
    which warning option do you use? -Wunused-value doesn't trigger the warning in my environment Mar 27, 2009 at 13:09
  • In VC++, it suppresses the warning
    – jalf
    Mar 27, 2009 at 13:10
45

The true reason for doing this dates back to a tool used on C code, called lint.

It analyzes code looking for possible problems and issuing warnings and suggestions. If a function returned a value which was then not checked, lint would warn in case this was accidental. To silence lint on this warning, you cast the call to (void).

2
  • 2
    It may have started this way, but most tools now have other mechanisms to suppress warnings such as this. Also - irrespective of why this started, within the domain of safety critical code in particular, this has become the usual way to "document" developer intentions. Mar 27, 2009 at 16:15
  • 7
    GCC gives a warning for this with -Wall.
    – greyfade
    Mar 27, 2009 at 18:18
27

Casting to void is used to suppress compiler warnings for unused variables and unsaved return values or expressions.

The Standard(2003) says in §5.2.9/4 says,

Any expression can be explicitly converted to type “cv void.” The expression value is discarded.

So you can write :

//suppressing unused variable warnings
static_cast<void>(unusedVar);
static_cast<const void>(unusedVar);
static_cast<volatile void>(unusedVar);

//suppressing return value warnings
static_cast<void>(fn());
static_cast<const void>(fn());
static_cast<volatile void>(fn());

//suppressing unsaved expressions
static_cast<void>(a + b * 10);
static_cast<const void>( x &&y || z);
static_cast<volatile void>( m | n + fn());

All forms are valid. I usually make it shorter as:

//suppressing  expressions
(void)(unusedVar);
(void)(fn());
(void)(x &&y || z);

Its also okay.

4
  • 2
    Except that it doesn't always turn off the compiler warnings. gcc doesn't seem respond to casting to void
    – hookenz
    Feb 11, 2013 at 3:29
  • @Matt: Which version of GCC are you using? Can you post your code at ideone.com or stacked-crooked.com (the latter is better). Feb 11, 2013 at 5:21
  • 1
    Does the standard "discarded" wording imply that warning should not be raised by linters? Jul 25, 2016 at 8:53
  • 2
    @CiroSantilli巴拿馬文件六四事件法轮功: .... that is an interesting question. Currently, I don't know the answer to that. Please shared with me if you get know it anyhow. Jul 25, 2016 at 9:08
14

C++17 or greater

[[maybe_unused]] auto unused = fn();

C++11 or greater

Update: this approach is not recommended as it could potentially break in future version of the spec. See this post for more discussion.

As of C++11 you can also do:

std::ignore = fn();

This should achieve the same result on functions marked with [[nodiscard]]

2
12

Since c++17 we have the [[maybe_unused]] attribute which can be used instead of the void cast.

1
  • 1
    C++17 also added nodiscard which may be of interest: stackoverflow.com/a/61675726/895245 Also I think you can't apply[[maybe_unused]] directly to the call it seems, only to a dummy variable that accepts the return of the call? If that is correct it is a bit wonky. May 8, 2020 at 9:15
6

Cast to void is costless. It is only information for compiler how to treat it.

1
  • 1
    It is "costless" if the time to write it and the subsequent time reading, understanding (and then ignoring) the code is free. IMO, writing (void) costs me time and energy and makes me wonder if the author knew how expressions work.
    – wallyk
    Jul 3, 2012 at 21:06
4

For the functionality of you program casting to void is meaningless. I would also argue that you should not use it to signal something to the person that is reading the code, as suggested in the answer by David. If you want to communicate something about your intentions, it is better to use a comment. Adding a cast like this will only look strange and raise questions about the possible reason. Just my opinion...

3
  • 3
    This is a known pattern to explicit tell others that you don't care about the return value, that you did not just forgot to handle it.
    – Spidey
    Oct 29, 2014 at 17:56
  • For the functionality of you program casting to void is meaningless For self-documentation and the number of warnings when compiling, it really isn't. you should not use it to signal something to the person that is reading the code [...] it is better to use a comment. The best comment is no comment, when the code explains itself. And, again, keeps a validly warning compiler happy in the process. Jul 19, 2016 at 14:03
  • 'Adding a cast like this will only look strange and raise questions about the possible reason'. I discovered one of these, and that's why I'm reading this. Seemed like mysterious stack-abuse to my untrained brain. Aug 18, 2017 at 15:29
4

C++17 [[nodiscard]]

C++17 standardized the "return value ignored business" with an attribute.

Therefore, I hope that compliant implementations will always warn only when nodiscard is given, and never warn otherwise.

Example:

main.cpp

[[nodiscard]] int f() {
    return 1;
}

int main() {
    f();
}

compile:

g++ -std=c++17 -ggdb3 -O0 -Wall -Wextra -pedantic -o main.out main.cpp

outcome:

main.cpp: In function ‘int main()’:
main.cpp:6:6: warning: ignoring return value of ‘int f()’, declared with attribute nodiscard [-Wunused-result]
    6 |     f();
      |     ~^~
main.cpp:1:19: note: declared here
    1 | [[nodiscard]] int f() {
      | 

The following all avoid the warning:

(void)f();
[[maybe_unused]] int i = f();

I wasn't able to use maybe_unused directly on the f() call:

[[maybe_unused]] f();

gives:

main.cpp: In function ‘int main()’:
main.cpp:6:5: warning: attributes at the beginning of statement are ignored [-Wattributes]
    6 |     [[maybe_unused]] f();
      |     ^~~~~~~~~~~~~~~~

The (void) cast working does not appear to be mandatory but is "encouraged" in the standard: How can I intentionally discard a [[nodiscard]] return value?

Also as seen from the warning message, one "solution" to the warning is to add -Wno-unused-result:

g++ -std=c++17 -ggdb3 -O0 -Wall -Wextra -pedantic -Wno-unused-result -o main.out main.cpp

although I wouldn't of course recommend ignoring warnings globally like this.

C++20 also allows you to add a reason to the nodiscard as in [[nodiscard("reason")]] as mentioned at: https://en.cppreference.com/w/cpp/language/attributes/nodiscard

GCC warn_unused_result attribute

Before the standardization of [[nodiscard]], and for C before they finally decide to standardize attributes, GCC implemented the exact same functionality with the warn_unused_result:

int f() __attribute__ ((warn_unused_result));

int f() {
    return 1;
}

int main() {
    f();
}

which gives:

main.cpp: In function ‘int main()’:
main.cpp:8:6: warning: ignoring return value of ‘int f()’, declared with attribute warn_unused_result [-Wunused-result]
    8 |     f();
      |     ~^~

It should be noted then that since ANSI C does not have a standard for this, ANSI C does not specify which C standard library functions have the attribute or not and therefore implementations have made their own decisions on what should or not be marked with warn_unuesd_result, which is why in general you would have to use the (void) cast to ignore returns of any calls to standard library functions to fully avoid warnings in any implementation.

Tested in GCC 9.2.1, Ubuntu 19.10.

3
  • You wrote "I wasn't able to use maybe_unused directly on the f() call", which is I think correct but in the standard I see the example: [[maybe_unused]] void f([[maybe_unused]] bool thing1, [[maybe_unused]] bool thing2) { [[maybe_unused]] bool b = thing1 && thing2; assert(b); }, wouldn't it be good to mention this in your answer as well?
    – albert
    Mar 3, 2022 at 17:40
  • @albert hi, we could cite the standard, but in this particular case, do you mean that the standard quote you gave implies something about the f(); call I mentioned? Mar 3, 2022 at 21:07
  • As far as I understand it when one uses [[nodiscard]]int f(){...} and in the call one doesn't need the return value one has to use (void)f(); or static_cast<void>f(); when having [[maybe-unused]]f(){...} one can use f();. So I think for completeness mentioning this possibility (with example) with example would be a good think. The style to be used is of course depending on what the user package has in its coding rules / preferences.
    – albert
    Mar 4, 2022 at 8:28
3

Also when verifying your code complies to MISRA (or other) standards, static-analysis tools such as LDRA will not allow you to call a function that has a return type without having it return a value unless you explicitly cast the returned value to (void)

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.