tags:

views:

715

answers:

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

+3  A: 

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.

David Holm
Does it suppress the warning about unused return values?
Paul Tomblin
Is there such warning?
Mykola Golubyev
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
I use g++ and it gives warning even with such cast.
klew
which warning option do you use? -Wunused-value doesn't trigger the warning in my environment
Mykola Golubyev
In VC++, it suppresses the warning
jalf
Without any options I get warnings: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result - but it started when I instaled newest Ubuntu. I didn't try to disable it yet.
klew
+1  A: 

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

klew
+4  A: 

David's answer pretty 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.
Richard Corden
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)
Johannes Schaub - litb
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
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.
Richard Corden
+5  A: 

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

Daniel Earwicker
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.
Richard Corden
GCC gives a warning for this with -Wall.
greyfade
+3  A: 

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

Dani van der Meer