How do I implement no-op macro in C++?
#include <iostream>
#ifdef NOOP
#define conditional_noop(x) what goes here?
#else
#define conditional_noop(x) std::cout << (x)
#endif
int main() {
conditional_noop(123);
}
I want this to do nothing when NOOP is defined and print "123", when NOOP is not defined.
A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).
In C, function-like macros are much similar to a function call. In this type of macro, you can define a function with arguments passed into it. TechVidvan Tutorial: Macros with arguments! In the above example, the compiler finds the name of the macro (AREA(a)) and replaces it with the statement (a*a).
You can't really get a high quality template work-alike in C with preprocessor macros; because, those macros expand only once, so at best you can get a data structure that can be retyped, but once processed is that type for the whole program.
Macro substitution is a mechanism that provides a string substitution. It can be achieved through "#deifne". It is used to replace the first part with the second part of the macro definition, before the execution of the program. The first object may be a function type or an object.
While leaving it blank is the obvious option, I'd go with
#define conditional_noop(x) do {} while(0)
This trick is obviously no-op, but forces you to write a semicolon after conditional_noop(123)
.
As mentioned before - nothing.
Also, there is a misprint in your code.
it should be #else not #elif. if it is #elif it is to be followed by the new condition
#include <iostream> #ifdef NOOP #define conditional_noop(x) do {} while(0) #else #define conditional_noop(x) std::cout << (x) #endif
Have fun coding! EDIT: added the [do] construct for robustness as suggested in another answer.
Defining the macro to be void
conveys your intent well.
#ifdef NOOP
#define conditional_noop(x) (void)0
#else
#ifdef NOOP
#define conditional_noop(x)
#elif
nothing!
#ifdef NOOP
static inline void conditional_noop(int x) { }
#else
static inline void conditional_noop(int x) { std::cout << x; }
#endif
Using inline function void enables type checking, even when NOOP
isn't defined. So when NOOP
isn't defined, you still won't be able to pass a struct to that function, or an undefined variable.
This will eventually prevent you from getting compiler errors when you turn the NOOP
flag on.
You can just leave it blank. You don't need to follow the #define
with anything.
Like others have said, leave it blank.
A trick you should use is to add (void)0
to the macro, forcing users to add a semicolon after it:
#ifdef NOOP
#define conditional_noop(x) (void)0
#else
#define conditional_noop(x) std::cout << (x); (void)0
#endif
In C++, (void)0
does nothing. This article explains other not-as-good options, as well as the rationale behind them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With