Inline function (5)

        Maybe we've heard of inline functions in C, but inline functions were first introduced in C++, and probably modern C compilers support inline functions.

        Const constants in C++ can replace macro constant definitions, such as: const int A = 3; <==> #define A 3; Is there a solution in C++ to replace macro code snippets? In C++, it is recommended to use inline functions instead of macro code snippets. Use the inline keyword to declare inline functions. When an inline function is declared, the inline keyword must be combined with the function definition, otherwise the compiler will ignore the inline request directly.

        The C++ compiler can inline compile a function, and the function that is inline compiled by the C++ compiler is called an inline function; the C++ compiler directly inserts the function into the place where the function is called, and the inline function does not have the extra overhead of ordinary function calls ( stack push, jump, return, etc.) ; but the C++ compiler will not necessarily satisfy the function's inlining request!

        Let's take the code as an example to analyze

#include <stdio.h>

#define FUNC(a, b) ((a) < (b) ? (a) : (b))

inline int func(int a, int b)
{
    return a < b ? a : b;
}

int main(int argc, char *argv[])
{
    int a = 1;
    int b = 3;
    int c = FUNC(++a, b);
    
    printf("a = %d\n", a);
    printf("b = %d\n", b);
    printf("c = %d\n", c);
    
    return 0;
}

        We define the macro function on line 3 to return the lesser of two numbers; on line 5 we define the function to return the lesser value. We first use the macro function to return, compare the values ​​of ++a and b on line 14, and obviously return 2, then the final value is a = 2, b = 3, c = 2; let's take a look at the result

picture.png

        What we see is that the return is 3. Looking at the program carefully, we call the macro function, that is to say, this ++a is executed twice, so it is normal to return 3. Let's replace the macro function call with a function call and see the result

picture.png

        We see that this time is consistent with what we analyzed, so there is still a difference between macro functions and functions, and macro functions have disadvantages.

        Let's talk about inline functions. Inline functions have the characteristics of ordinary functions (parameter checking, return type, etc.); the inline request of the function may be rejected by the compiler; after the function is inline compiled, the function body is directly expanded to Where it is called; macro snippets are processed by the preprocessor with simple text replacement without any compilation process, so side effects may occur .

        Modern C++ compilers can perform compilation optimizations, and some functions may be compiled inline even without inline declarations; some modern C++ compilers have extended syntax to force functions to be inlined, such as: g++: __attribute__((always_inline )) attribute; MSVC: __forcrinline

        Let's take the code as an example to analyze

#include <stdio.h>


inline 
int add_inline(int n);

int main(int argc, char *argv[])
{
    int r = add_inline(10);

    printf(" r = %d\n", r);

    return 0;
}

inline int add_inline(int n)
{
    int ret = 0;

    for(int i=0; i<n; i++)
    {
        ret + = i;
    }

    return ret;
}

        We entered the disassembly through breakpoint debugging in VS2010 to see if the inline function took effect. By calling the inline function add_inline on line 8, we see that the assembly function is as follows

picture.png

        We see a call with call, proving that the inline request failed. The function is still called, and the function body is not directly spread in this block. This also proves what we said before that not every inline request will be executed.

        There are several conditions for inline compilation in C++: a> there cannot be any form of loop statement; b> there cannot be too many conditional judgment statements; c> the function body cannot be too large; d> the function cannot be fetched address operation; e> function inline declaration must be before the function call statement;

        Through the study of inline functions, the summary is as follows: 1. In C++, inline functions can be declared through inline; 2. The compiler directly expands the inline function body to the place where the function is called; 3. Inline is just a request, the compiler This kind of request is not necessarily allowed; 4. The inline function saves the overhead of pushing the stack, jumping and returning when the function is called.


        Welcome to learn C++ language together, you can add me QQ: 243343083 .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325387917&siteId=291194637