Macros vs. Functions
- Functions must have a type associated with their parameters.
- Macros do not have to have a type associated with their parameters.
- Functions have overhead associated with each call.
- Macros do not have any overhead.
- Functions behave correctly (i.e. as expected) no matter what the actual
parameters are.
- Macros do not always behave as expected if the parameters are expressions.
- Functions can be as complicated as needed.
- Macros should be very simple and should NOT contain a loop. Many
compilers will not accept macros with loops. Basically, if the macro
can not be written in 1 line, it probably should be a function.
- Most (if not all) of the function's overhead can be eliminated by
using the C++ inline facility.
- To make the macros behave, the programmer must force a type constraint
and use a global variable. (See Foster Example 11-16 on page 602.)