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
Post a Comment