Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement no-op macro (or template) in C++?

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.

like image 383
Anton Daneyko Avatar asked Aug 20 '09 14:08

Anton Daneyko


People also ask

What is macro template in C?

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(;).

Can we use macro for function in C?

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).

Can you make templates in C?

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.

What is macro replacement in C?

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.


7 Answers

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).

like image 133
avakar Avatar answered Oct 12 '22 23:10

avakar


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.

like image 28
Andrew Avatar answered Oct 12 '22 22:10

Andrew


Defining the macro to be void conveys your intent well.

#ifdef NOOP
    #define conditional_noop(x) (void)0
#else
like image 34
Andrew Keeton Avatar answered Oct 12 '22 22:10

Andrew Keeton


 #ifdef NOOP       
     #define conditional_noop(x)   
 #elif  

nothing!

like image 31
Toad Avatar answered Oct 12 '22 22:10

Toad


#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.

like image 27
Nicolas Viennot Avatar answered Oct 12 '22 22:10

Nicolas Viennot


You can just leave it blank. You don't need to follow the #define with anything.

like image 38
Justin Avatar answered Oct 13 '22 00:10

Justin


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.

like image 42
GManNickG Avatar answered Oct 13 '22 00:10

GManNickG