Inline Functions in C++

When a normal function is called in C++, some are overheard just for calling the function. Actually, calling a function takes time before the function really starts executing. This time between when the function is called and when the function really starts executing is called the switching time. If the normal function is large, meaning it will take a long to execute, then the switching time is relatively small, and the user can ignore it. However, if the normal function is small, as many functions are, then the switching time, roughly the same for large and small functions, should not be ignored. In many situations, the switching time is longer than the time the small function takes to really execute; in some cases, much longer.

To solve this problem, C++ uses macro and the inline function. A macro is like a small function, but it is usually shorter than a typical small function. The longest macro is still one “statement”. A function body can have more than one statement. A small inline function has advantages over a normal small function.

When a macro is defined, it is called later down in the program. An inline function is also defined and then called later down in the program. A normal function is defined and then called later down in the program. All these three types are defined and called later down in the program. Any of them can be called more than once.

The macro and small inline functions are different from the normal approach function when they are later down in the program. The C++ compiler places the defined macro code or the defined small inline function code (body) wherever it is called down in the program. When the compiler does this, the compiler is said to have expanded the macro or the inline function. This is not the case for a normal function. The normal function is not expanded, where it is called.

While the call of a normal function needs switching time, for the function to be properly asserted before its execution, the macro or small inline function begins execution whenever it is called, and there is no switching time waste. That is the main advantage the macro and the small inline function have over the normal function, i.e., switching time is omitted.

This article explains inline functions in C++ compared to macros. An explanation of the macro is given. A comparison of inline function and normal function is done towards the end of the article.

Note: Calling a macro in a program is said to be invoking the macro.

Article Content

Defining Macros and inline Functions

Object-like Macro and inline variable
There is an object-like macro, and there is a function-like macro. Correspondingly, there are inline variables and inline functions. Consider the following C++ program:

#include
using namespace std;

#define var1 "E"

inline char var2 = 'E';

int main()
{
        cout << var1 << endl;
        cout << var2 << endl;

        return 0;
}

The output is:

    E
    E

This program has an object-like macro and an inline variable. Each holds the value, ‘E’. An object-like macro begins with #define and has no type indicator. An inline variable begins with “inline,” and a type indicator follows it. Macros have a disadvantage compared to inline types because they do not indicate the type. This can lead to type mismatch problems down in the program. In the main() function, var1 and var2 are the definition code of the different variables, respectively.

Note: it is not clear if var1 holds a char or a literal string. Also, note that a macro, whether object-like or function-like, does not end with a semicolon. It ends by pressing the Enter key. An inline variable or inline function ends in their respective normal way.

Function-like Macro and inline Function
A function-like macro is a macro that takes arguments. Like the object-like macro, wherever the function-like macro is invoked down in the program, the compiler replaces the invocation with the code definition and eliminates the switching time (function call overhead) at run time.

An inline function is a function that begins with “inline”. It has an advantage over the function-like macro with its return type and argument types. A function-like macro does not have argument types or return types. Its return type is the final value of the macro name.

The following C++ program has a function-like macro and an inline function, each of which looks for the maximum value of two arguments. The inline function compares two integers and returns the bigger integer. The return value of the inline function can be assigned to a new int variable. On the other hand, the final value of the macro becomes the value of the macro.

#include
    using namespace std;

    #define maxM(a, b) ((a) > (b) ? (a): (b))

    inline int maxI(int a, int b) {
        if (a > b)
            return a;
        if (a < b)
            return b;
        if (a == b)
            return a;
    }

    int main()
    {
        cout << maxM(2.5, 6) << endl;
        cout << maxI(3, 7) << endl;

        return 0;
    }

The output is:

    6
    7

With the macro, the arguments should be of compatible types. This gives the macro a kind of advantage over the inline function, whose argument types should be the same, in this case.

The name of the macro is maxM. The arguments are a and b. The rest is a kind of function body, delimited by parentheses. It says if (a) > (b) is true, then a becomes the value of the macro; otherwise, b becomes the value of the macro.

inline Function and the Compiler

After the compiler substitutes the inline function call with the definition code of the function, the program still has to run. The compilation is not running or executing the program. With the normal function, overhead (switching time) occurs when the program is run (executed). Macro or inline replacement occurs during compilation, which is before execution (before the program is sent to the customer or user).

In the end, the switching time is omitted or gained for macros and small inline functions. However, if the inline function is big, the compiler will decide whether to make the function declared as inline, inline, or not. If the function declared as inline is big, there might be no significant gain in replacing any of its calls with the body of its function code. As for the criteria of the compiler decision, – see later.

Note: A function defined within a class definition is an inline function preceded by the inline specifier.

Comparing Macros and inline Functions

The macro can work with different types as long as they are compatible. This is an advantage. However, that also leads to side effects, which then gives it a disadvantage. The inline function will test the validity of its argument types before using the arguments, which prevents side effects.

Comparing inline and Normal Functions

Advantages of inline Function

  • There is no function call overhead (no switching time).
  • There is also overhead when a normal function returns. With the inline function, there is no return call overhead.
  • There is possible context-specific optimization of the function body with the inline function.

Disadvantages of inline Function

  • For each call of the inline function, the function definition (body) code is repeated (was retyped by the compiler). This can lead to a very large, binary (compiled) file.
  • The compiler takes a long to compile, as it repeats the same code for all the calls.

Inline functions may not be needed for many embedded systems because a smaller program size is preferable to a higher speed.

There are other disadvantages – see later.

Conclusion

The inline function is like a macro. They serve the same purpose. The definition code replaces each invocation or function call. However, an inline function has more advantages over the macro. There is the object-like macro, and there is correspondingly the inline variable.  There is the function-like macro, and there is correspondingly the inline function. A function defined within a class definition is an inline function, whether the inline specifier precedes it or not.

To define an object-like macro or a function-like macro, precede it with #define followed by the macro name. The macro does not specify its value type or argument types. To define an inline variable or an inline function, precede it with the specifier, inline, followed by the return type, and then the name. For the inline function, both the return type and the argument types are precise. Side effects are prevented.

The inline function has overall advantages over the macro. There are pros and cons when the inline function is compared to the normal function.



from https://ift.tt/2Wvqf8S

Post a Comment

0 Comments