Tutorial :When are C++ macros beneficial? [closed]



Question:

The C preprocessor is justifiably feared and shunned by the C++ community. In-lined functions, consts and templates are usually a safer and superior alternative to a #define.

The following macro:

#define SUCCEEDED(hr) ((HRESULT)(hr) >= 0)    

is in no way superior to the type safe:

inline bool succeeded(int hr) { return hr >= 0; }  

But macros do have their place, please list the uses you find for macros that you can't do without the preprocessor.

Please put each use-cases in a seperate answer so it can be voted up and if you know of how to achieve one of the answers without the preprosessor point out how in that answer's comments.


Solution:1

As wrappers for debug functions, to automatically pass things like __FILE__, __LINE__, etc:

#ifdef ( DEBUG )  #define M_DebugLog( msg )  std::cout << __FILE__ << ":" << __LINE__ << ": " << msg  #else  #define M_DebugLog( msg )  





        
Previous
Next Post »