Concept behind Inline Functions and uses

Share the Article

What are inline functions?

The concept behind inline functions is to replace each call of that function with its code body, and this is likely to increase the size of object code, also known as code bloat.

Impact: On machines with limited memory, unnecessary inlining can give rise to big executables that are too large for the available space. Due to this inline-induced code bloat, impact will be seen on instruction cash hit rate. This ultimately leads to or it may lead to additional paging. In the end, this may slow down the program.

Inlining is good for those functions who have very small body so that cost of the code generation for function call should be higher that replacing the call with its body. Ultimately, this shall have better runtime performance.

There are 2 ways to make a function inlined:

Implicit inlining

C++ Programs achieve Implicit inlining by writing the definition of member functions inside the body of class.

Class MainFunda { int x; public: int getX() // Implicit inline function { return x; } };

Explicit inlining

Programs achieve explicit inlining when they explicitly write the “inline keyword” before the definition of any function.

inline int max (int a, int b) // explicit inline function { return (a>b)?a:b; }

How inlining is decided by compiler

Asking the compiler to treat a function inline is only a request and does not guarantee that it will be done. The compiler can reject the request to inline in some specific cases. Example.

  • When the body of function is too big. Like, those that contain loops or there are recursive calls.
  • Secondly, when the program tries to get the address of a function. In this case, the compiler will not be able take an address if the funtion is inline. This is because, the inline function do not exist at one location. There are multiple copies of function in different places of call.
  • Thirdly, when the function is virtual. The compiler uses virtual functions to implement run-time polymorphism. Therefore, it saves the address of such virtual functions in a vtable. This approach is contradictory to inline. Hence, compiler cannot inline such a function.
  • Constructor and destructors are very bad candidates for inlining because lot of invisible code may be present to enable initialization and clean-up of member variables and other items (like, base-classes, vptr)

Impact of making functions inline in 3rd party library

The library designer has to take decision to inline any function very carefully. Because, all inline functions in header file will become part of client’s executable. Therefore, user will have to recompile the whole code, if library owner provide any changes in the future. On doing re-compilation, the inline functions of library will again become part of the client code. This may lead complications for entire system. If the library dynamically links the containing the function, then integration with user code is transparent to clients.

Main Funda: Generally, Inline functions are good as long as they are small (one or two line only)

Related Topics:

What are differences : #define & const built-in types?
Understanding Constant Variables
Why variable declaration should not be done at the start? 
Understanding array version of new[] & delete[]
How to overload global version of new & delete ?
How to overload new & delete for a class?
Explaining C++ casts
How to overload placement new operator?
Benefit of using “override” keyword?
What are Lambda?
How to stop compiler from generating special member functions?
 What are the drawbacks of using enum ?
Parametrized constructor
What is an explicit constructor ?
Basics of throwing and catching exception


Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *