Inline functions versus preprocessor macros
Question
How do inline functions differ from macros in C++?
Short answer
Macros are text substitution by the preprocessor; inline functions are real, type-checked functions. A macro ignores types and scope, may evaluate an argument several times (so SQUARE(i++) misbehaves) and needs heavy bracketing to avoid precedence bugs; it is also invisible to the debugger. An inline function evaluates each argument once, respects types, scope and overloading, and inline is only a hint the compiler may ignore.
Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.3, p.226; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solution