Inline function in C++
One of the key features of C++ is the inline function. Therefore, let's first examine the utilization of inline functions and their intended application. If make a function is inline, then the compiler replaces the function calling location with the definition of the inline function at compile time.
Any changes made to an inline function will require the inline function to be recompiled again because the compiler would need to replace all the code with a new code; otherwise, it will execute the old functionality.
In brief, When the program executes a function call instruction, the CPU copies the function's arguments to the stack, caches the memory address of the next instruction and then hands control to the targeted function. The CPU then performs the function's code, saves the return value in a designated memory address or register, and hands control back to the function that is called the function. This may become overhead if the function's execution time is shorter than the time required to move from the calling function to the called function (callee). The overhead of the function call is typically negligible compared to the length of time required to run large or complicated functions. The time needed to call a small, frequently used function, however, is often far longer than the time necessary to run the function's code. Due to the fact that their execution time is less than switching time, tiny functions experience this overhead. To minimize the overhead of function calls, C++ offers inline functions. When a function is invoked, it expands in line and is known as an inline function. When an inline function is invoked, its entire body of code is added or replaced at the inline function call location. At compile time, the C++ compiler makes this substitution. Efficiency may be increased by inline function if it is tiny. To the compiler, inlining is merely a request; it is not a command. The compiler may reject the inlining request. The compiler may not implement inlining in situations like these:
Syntax for an inline function:
Let's understand the difference between the normal function and the inline function.
Inside the main() method, when the function fun1() is called, the control is transferred to the definition of the called function. The addresses from where the function is called and the definition of the function are different. This control transfer takes a lot of time and increases the overhead.
When the inline function is encountered, then the definition of the function is copied to it. In this case, there is no control transfer which saves a lot of time and also decreases the overhead.
Let's understand through an example.
Once the compilation is done, the code would be like as shown as below:
Why do we need an inline function in C++?
The main use of the inline function in C++ is to save memory space. Whenever the function is called, then it takes a lot of time to execute the tasks, such as moving to the calling function. If the length of the function is small, then the substantial amount of execution time is spent in such overheads, and sometimes time taken required for moving to the calling function will be greater than the time taken required to execute that function.
The solution to this problem is to use macro definitions known as macros. The preprocessor macros are widely used in C, but the major drawback with the macros is that these are not normal functions which means the error checking process will not be done during the compilation.
C++ has provided one solution to this problem. In the case of function calling, the time for calling such small functions is huge, so to overcome such a problem, a new concept was introduced known as an inline function. When the function is encountered inside the main() method, it is expanded with its definition thus saving time.
We cannot provide the inlining to the functions in the following circumstances:
When do we require an inline function?
An inline function can be used in the following scenarios:
Advantages of inline function
Disadvantages of inline function
The following are the disadvantages of an inline function:
What is wrong with the macro?
Readers who are familiar with C are aware that it uses macros. All direct macro calls within the macro code are replaced by the preprocessor. It is advised to always use inline functions rather than macros. The inventor of C++, Dr. Bjarne Stroustrup, claims that macros are rarely required in C++ and are error-prone. The use of macros in C++ is not without issues. Private class members are inaccessible to macro. Although macros resemble function calls, they do not truly do so.
The C++ compiler verifies that the necessary conversions are made and that the inline functions' parameter types are valid. A preprocessor macro cannot accomplish this. Additionally, the preprocessor manages macros, while the C++ compiler manages inline functions. Keep in mind that while it is true that all functions specified inside a class are implicitly inlined and that the C++ compiler will execute inline calls to these functions, inlining cannot be performed if the function is virtual. The cause is that a virtual function's call is resolved at runtime rather than at compile time. If the compiler doesn't know which function will be called, how can it conduct inlining during compilation when virtual means waiting till runtime? Another thing to keep in mind is that moving a function intoline will only be beneficial if the time required to call the function is longer than the time required to execute the function body. As an illustration, consider the following:
Executing the aforementioned function takes a while. The basic rule is that a function that performs input output (I/O) operations shouldn't be defined as inline because it takes a lot of time. Since the I/O statement would take far longer than a function call would, technically, inlining the show() function is only of limited utility.
If the function is not expanded inline, depending on the compiler you are using, a warning may be displayed. Inline functions cannot be used in programming languages like Java or C#.
But because final methods cannot be overridden by subclasses and calls to final methods are handled at compile time, the Java compiler can conduct inlining when the small final process is called. By inlining short function calls, the JIT compiler can further optimize C# code (like replacing body of a small function when it is called in a loop).