Disadvantages of Macro compared to functions:
- Works on text substituiton mechanism by the preprocessor unlike normal functions where assembly-language Call and Return are used.
- Preprocessor macros cannot access class member data and hence they cannot be used as class member functions.
- Generally confined to functions that can be expressed in a single statement.
- No type checking is done for macros.
Inline function:
- C++ implements macro as inline function.
- Function definition inside a class definition is automatically inline.
- Inline function is expanded in place, like a preprocessor macro so that overhead of function call is eliminated.
- The keyword inline is just a suggestion to the compiler to make the function inline. There are cases when the inilining cannot be performed:
- Compiler cannot perform inlining if the function is too complicated.
- Compiler cannot perform inlining if the addess of the function is taken implicitly or explicitly. If the compiler must produce an address, then it will allocate storage for the function and use the resulting address.
- Compiler does not perform inlining if are static variables used in the function.
- Compiler does not perform inlining if there is a recursive function call.
- Inline functios have internal linkage. The keyword extern has no significance when used with inline functions.