C-style vs Function-style: Best Cast for the Unused Variable/Function Warning

When using a cast to void in order to suppress/silence a warning for an unused variable or function there are different possible combinations:

static int foo(){ return 4; }

int main(){
    int x;

//   void   x;      // Error: void variable
//   void  (x);     // Error: void variable
    (void)  x;      // OK
    (void) (x);     // OK

//   void   foo();  // Error: new function declaration
//   void  (foo()); // Error: new function declaration
    (void)  foo();  // OK
    (void) (foo()); // OK

//   void   foo;    // Error: void variable
//   void  (foo);   // Error: void variable
    (void)  foo;    // OK
    (void) (foo);   // OK

    return 0;
}

In order to be able to use the second combination (01), that is, function-style cast, I tried enclosing the expression in parentheses and it seems to be correct:

(void  (x));
(void  (foo()));
(void  (foo));

This strategy cannot make the first combination (00) work as a cast either, and I interpret it is not needed neither in the third nor in the fourth. Of course, even more extra meaningless parentheses could be added. Actually, if my understanding is correct, the fourth combination (11) contains indeed such unnecessary parentheses, and it is just the same C-style cast as the third combination (10).

My question is which of these approaches is the best, making sure it is not optimized out by the compiler, while understanding that no extra needless parentheses are used. I did not explore in detail the syntax expansion of this kind of expressions, so maybe I am missing something.

Note that I will not use this construct in any macro, but directly on a variable/function. Besides, I am not concerned about side effects on volatile variables.

Links:

  • Is a C-style cast identical to a function-style cast?
  • Universally compiler independent way of implementing an UNUSED macro in C/C++
  • Will a “variableName;” C++ statement be a no-op at all times?
  • How do I best silence a warning about unused variables?
  • Suppress Compiler warning Function declared never referenced
  • How can I hide “defined but not used” warnings in GCC?

  • 1

    I was aware of that attribute, however, to the best of my knowledge, it is only available since C++17 onwards.

    – 

  • 2

    the second combination (01) I don’t understand that, what the second combination is and what 01 is.

    – 

  • 3

    Generally if you ever feel the need to do a C-style explicit conversion (cast), you should take it as a sign that you’re doing something wrong. The problem in your case is that you should use one of the C++-specific explicit conversions, like static_cast.

    – 

  • 1

    “making sure it is not optimized out by the compiler” — Why? Your stated goal is “to suppress/silence a warning”. I’d think that once that is accomplished, you’d want the unnecessary code to be optimized out by the compiler.

    – 

  • 2

    @Someprogrammerdude I think this might be the exception to the rule. (void) x sits just fine with me.

    – 

Leave a Comment