Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

If I define a function macro with no actual function, 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?
share|improve this question
add comment

4 Answers 4

up vote 10 down vote accepted

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
share|improve this answer
    
this is very usefull answer i think good job ;) –  DarioOO Dec 31 '12 at 14:15
    
Hi I do n't understand your first code Here I tried .. I think its just an error to write int foo(int UNUSED(value)) if macro definition is balank ? –  Grijesh Chauhan Mar 8 '13 at 23:09
1  
@GrijeshChauhan, you've just discovered one of the differences between C and C++. See here: codepad.org/flX8m3sk –  Mark Ransom Mar 8 '13 at 23:16
    
Thanks Got it :) nice working.. actually I asked a question a day back. –  Grijesh Chauhan Mar 8 '13 at 23:20
    
In addition to I got answer and your answer I got one more usefulness of macro without body here THROW_ .. –  Grijesh Chauhan Mar 8 '13 at 23:26
add comment

Your code is not totally correct, I suggest you to put empty braces in your macro

#define somemacro(a) {}

the reason is simple, you code will be much more safe!

take this example:

if(Value)
    somemacro(a)
else
    somemacro(b)

If the macro is empty, your code will not compile! (Expected primary-expressione before "else"). Anyway certain style rules force you to write

if(Value)
{
    somemacro(a)
}
else
{
    somemacro(a)
}

so that will not be a problem.

Another option is to use ";" instead of "{}" but that option in the same case will give you compile time warnings, while empty braces will not give warnings nor errors! (semicolon is still better even if give warnings) ;)

take following case

if(value)
    somemacro(a);
else
    somemacro(b);

will expand to

if(value)
{};
else
{};

that can't compile!

That's why macros are evil

(since macros are simple text-replacement, the rule of dumbs should be to always try to manually replace the code and see what happens, there are also tools that will replace macros for you showing the expanded code.)

Still guessin if there is a macro that is totally safe? Yes it is called "NOP"

#define somemacro(a) ((void)0)

that will work in any case (even source files of compilers use that as NOP, for example just look at "assert.h"

share|improve this answer
    
The code is perfectly fine. Safety was not the original inquisition. Also, NOP is not a C++ term, but rather an ASM bytecode that can be synthesized using ((void)0). –  Qix Dec 27 '12 at 2:47
    
The exact intend was to show a most correct usage of macros. a empty function macro can't still be used in certain cases. So any random user reading you post must be aware that using a "((void)0)" is better choice. And anyway that's a "NOP" but not a NOP. infact no extra assembly is generated for ((void)0) while a NOP instruction is still assembly code. Safety of code should always be considered. Still no reason for thumbs down anyway. –  DarioOO Dec 31 '12 at 14:08
    
There is no "correct usage of macros" - macros are simply a tool. If you know how the macro should be used then however you use it is correct. They're not a magical thing and there isn't just one way to use them. –  Qix Jan 8 '13 at 17:31
add comment

That's correct. Your code expands to

;

after preprocessing.

Note that you can ask your compiler to show you the code after preprocessing (in gcc, this is the -E option; your compiler may vary).

share|improve this answer
    
The code doesn't generate anything, not even a semi-colon. (verified with gcc 4.5.2) –  shinkou Feb 8 '12 at 4:38
1  
@shinkou, the semi-colon is outside the macro so it should be part of the final code. –  Mark Ransom Feb 8 '12 at 4:41
add comment

The preprocessor performs literal substitution with all macros.

Therefore, if you define an "empty" macro, then each place that identifier appears in your code will be replaced with an empty statement by the preprocessor before the compiler ever runs.

So yes. No code will be generated for the example given in your question.

share|improve this answer
    
"before the compiler ever runs" Keeping in mind that the generic C/C++ preprocessor is contained within the compiler itself, just not the compiler part of the compiler ;) Could be misunderstood. –  Qix Jan 8 '13 at 17:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.