8

Possible Duplicate:
casting unused return values to void

I read some source code, and in it many virtual functions in the interface classes are declared and default-implemented as such:

virtual bool FunctionName(TypeName* pointer)
{
   (void)pointer;
   return true;
}

May I ask what is the purpose of casting the pointer to void in the default implementation?

Community
  • 1
  • 1
Jake
  • 11,273
  • 21
  • 90
  • 147
  • 2
    This should not compile, as the function needs to return a value. – Björn Pollex Nov 14 '10 at 17:34
  • 1
    That was asked around numerous times. Casting something to `void` indicates you're not using the return value. In your case, it's a no-op. – zneak Nov 14 '10 at 17:35
  • @zneak: `pointer` isn't a return value. Not a dupe of that question, although I'm sure this has been covered before for parameters too. – Steve Jessop Nov 14 '10 at 17:46
  • 1
    What I don't understand here is why the code isn't just `FunctionName(TypeName*) { }` (maybe with a return value). What compiler would spit out an "unused" warning for that? – Steve Jessop Nov 14 '10 at 17:48
  • @Steve that's an virtual function with an inline definition, so if it's a base class which has do-nothing definitions, then you still want the names of the arguments for documentation purposes. – Pete Kirkham Nov 14 '10 at 18:14
  • @Steve Jessop The expression `pointer;` alone returns the value of `pointer`. Casting that to void ignores the return value of the expression, and as such does nothing. The cast behaves the same as if you casted the return value of a function (minus the effects of the function itself, obviously). – zneak Nov 14 '10 at 18:57
  • @zneak: expressions don't have return values, they have results. I think that the reasons you would cast a return value to void are different from the reasons that you would write a statement just for the purpose of casting a parameter to void. One is normally for the attention of developers (to show that you're deliberately ignoring a possible error), the other is normally for the attention of the compiler (to suppress a warning). Only one of the answers to that other question has anything to do with this one, and that's the one mentioning lint. Still, you've won the vote, so fair enough. – Steve Jessop Nov 14 '10 at 19:49

2 Answers2

20

Multiple purposes depending on what you cast

  • Marking your intention to the compiler that an expression that is entirely a no-op is intended as written (for inhibiting warnings, for example)
  • Marking your intention to to the compiler and programmer that the result of something is ignored (the result of a function call, for example)
  • In a function template, if a return type is given by a template parameter type T, and you return the result of some function call that could be different from T in some situation. An explicit cast to T could, in the void case, prevent a compile time error:
    int f() { return 0; } void g() { return (void)f(); }
  • Inhibiting the compiler to choose a comma operator overload ((void)a, b will never invoke an overloaded comma operator function).

Note that the Standard guarantees that there will never be an operator void() called if you cast a class object to void (some GCC versions ignore that rule, though).

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
13

In this case it's just to avoid compiler's warning about unused parameter.

ruslik
  • 14,714
  • 1
  • 39
  • 40
  • +1: for giving the correct answer – Paul R Nov 14 '10 at 19:07
  • Why make such a parameter in the first place? – lost_in_the_source Mar 18 '14 at 22:58
  • 1
    The parameter could occur because some other class that also implements this virtual function actually does use that parameter. – David K Jul 24 '14 at 11:29
  • Example: In one of your subclasses, you expect a parameter x of a virtual function to be always zero. You write an assert (x == 0) but don't use x otherwise. (void) x has a good chance of compiling an a debug and a release version without warnings. – gnasher729 Nov 03 '14 at 18:45