Asked  6 Months ago    Answers:  5   Viewed   26 times
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?

Follow up:

Well that seems pretty comprehensive. I suppose it's better than commenting an unused return value since self documenting code is better than comments. Personally, I'll turn these warnings off since it's unnecessary noise.

I'll eat my words if a bug escapes because of it...

 Answers

52

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.
Tuesday, June 1, 2021
 
PHLAK
answered 6 Months ago
85

It is there to avoid warnings from the compiler because some parameters are unused.

Tuesday, June 1, 2021
 
Troncoso
answered 6 Months ago
57

In C, pointer conversions to and from void* were always implicit.

In C++, conversions from T* to void* are implicit, but void* to anything else requires a cast.

Wednesday, June 9, 2021
 
superfell
answered 6 Months ago
28

Yes, as far as i know the second declaration is invalid in C++ and C89, but it is valid in C99.

From The C99 draft, TC2 (6.7.5.3/10):

The special case of an unnamed parameter of type void as the only item in the list speci?es that the function has no parameters.

It's explicitly talking about the type "void", not the keyword.

From The C++ Standard, 8.3.5/2:

If the parameter-declaration-clause is empty, the function takes no arguments. The parameter list (void) is equivalent to the empty parameter list.

That it means the actual keyword with "void", and not the general type "void" can also be seen from one of the cases where template argument deduction fails (14.8.2/2):

  • Attempting to create a function type in which a parameter has a type of void.

It's put clear by others, notable in one core language issue report here and some GCC bugreports linked to by other answers.


To recap, your GCC is right but earlier GCC versions were wrong. Thus that code might have been successfully compiled with it earlier. You should fix your code, so that it uses "void" for both functions, then it will compile also with other compilers (comeau also rejects the second declaration with that "VOID").

Friday, June 25, 2021
 
MGP
answered 6 Months ago
MGP
33

The javadoc of Comparator.comparing() says:

The returned comparator is serializable if the specified function is also serializable.

The cast ensures that the internal class used by Java to implement the lambda will implements Serializable.

Tuesday, August 17, 2021
 
leetwinski
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share