What is meant by inline fucntion in C++?
Inline function is a kind of optimization method which is made use by compilers.
You need to pre-append the keyword, ‘inline’ in front of function declaration for making the function as inline.
The inline methods will provide instructions for compiler for inserting the function body wherever the function is used inside the program.
Advantages of inline functions:
- Inline methods never involve any overhead of function call.
- Overhead of stack operations like push or pop during function calling is avoided.
- Inline functions also reduce the overhead of function return calls.
- By using the instruction caches, locality of reference is increased.
- Intra-procedural optimization shall also be specified after in-lining the compiler. This is the most essential advantage as in this way the compiler gets focused on elimination of dead coding and can stress more on elimination of induction variable, branch prediction, etc.
Disadvantages of inline functions:
- Inline function increases method size which never fits within cache and this causes many cache miss.
- If the number of register variables increases after in-lining the functions, it creates resource utilization overhead.
- Compilation overhead will be created by inline functions.
- When the inline functions are used in header files, the size of these header files increases and also made unreadable.
- Inline functions use huge code size resulting in memory thrashing. Page faults are also caused which degrades the performance of your program.
- Inline functions are never helpful with embedded systems as binary system is not compatible with inline functions because of memory constraints.