What is the difference between a preprocessor macro with no arguments, and one with zero arguments
Is there any reason to prefer
#define MY_MACRO() ..stuff..
to
#define MY_MACRO .开发者_如何转开发.stuff..
Don't use macros is not a valid answer...
Replacement only occurrs for a function-like macro if the macro name is followed by a left parenthesis. So, the following all invoke the function-like macro MY_MACRO()
:
MY_MACRO()
MY_MACRO ( )
MY_MACRO
( )
But this would not:
MY_MACRO SomethingElse
It depends on how you are using the macro and what it is used for as to whether or not this is important. Ideally, your macros will all have distinct names; if you reserve all-uppercase identifiers for macros, then it shouldn't matter whether you use an object-like or a function-like macro with zero parameters.
Aesthetically, it's usually (but not always) cleaner not to have function-like macros that take zero parameters.
I prefer to use MY_MACRO()
, with parentheses, because it feels more like I'm calling a function. Otherwise it looks like I'm calling a constant:
MY_MACRO();
vs
MY_MACRO;
That is if the definition is used to call code, rather than just a simple constant value.
Macro( x ) is preferrable when you have parameters to pass. If you are doing a simple "replace" then, IMO, its preferrable to use #define BLAH ...
精彩评论