Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty function macros

If I define a function macro with no actual body, is it like an empty string with the compiler (i.e. It doesn't generate any extra instructions at compile time)?

Example:

#define SomeMacro(a)  SomeMacro("hello"); // This line doesn't add any instructions, does it? 
like image 226
Qix - MONICA WAS MISTREATED Avatar asked Feb 08 '12 03:02

Qix - MONICA WAS MISTREATED


1 Answers

You're absolutely correct, the empty macro doesn't generate any code.

I've seen two places where this is useful. The first is to eliminate warnings when a function parameter isn't used:

#define UNUSED(x)  int foo(int UNUSED(value)) {     return 42; } 

The second is when you use conditionals to determine if there should be code or not.

#ifdef LOGGING_ENABLED #define LOG(x) log_message(x) #else #define LOG(x) #endif 
like image 121
Mark Ransom Avatar answered Sep 18 '22 06:09

Mark Ransom