Preprocessor

Why not macros?

Preprocessor macros are not recommended for C++. Use them only if you have to (for debugging or tracing).

Do/While in macros

When you need to specify several instructions inside macro, there is a trick to define this macro in a way that allows to use the macro as a standard function. This trick is implemented using single-step do/while loop which ends on first check as follows:

#define MY_MACRO(a,b) do { perform1(a); perform2(a,b); } while(0)

With this construction you are able to use your macro in if/else statements as follows:

if (condition)
   MY_MACRO(2,8);
else
   other_processing();

See more here.

Variadic Macros

Use them when your macro needs to handle variable number of arguments.

Pre-defined macros

Share

follow