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.
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
Have fun coding! EDIT: added the [do] construct for robustness as suggested in another answer.
As this is a macro, you should also consider a case like
to be on the safe side, you can give an empty statement like
for older C sometimes you need to define the empty statment this way (should also get optimized away):
While leaving it blank is the obvious option, I'd go with
This trick is obviously no-op, but forces you to write a semicolon after
conditional_noop(123)
.