c preprocessor - When are C++ macros beneficial? -


the c preprocessor justifiably feared , shunned c++ community. in-lined functions, consts , templates safer , superior alternative #define.

the following macro:

#define succeeded(hr) ((hresult)(hr) >= 0)   

is in no way superior type safe:

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

but macros have place, please list uses find macros can't without preprocessor.

please put each use-cases in seperate answer can voted , if know of how achieve 1 of answers without preprosessor point out how in answer's comments.

as wrappers debug functions, automatically pass things __file__, __line__, etc:

#ifdef ( debug ) #define m_debuglog( msg )  std::cout << __file__ << ":" << __line__ << ": " << msg #else #define m_debuglog( msg ) #endif 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -