Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cast unused return values to void?

Tags:

c++

c

void

People also ask

What does casting to void do in C?

Casting to void is used to suppress compiler warnings. The Standard says in §5.2. 9/4 says, Any expression can be explicitly converted to type “cv void.” The expression value is discarded. Follow this answer to receive notifications.

Why void is not return type?

Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.

Can you return a value from void?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.

Why are return values useful?

Generally, a return value is used where the function is an intermediate step in a calculation of some kind. You want to get to a final result, which involves some values that need to be calculated by a function.


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.

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.


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).


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.