c++ inline function?

前端 未结 6 460
终归单人心
终归单人心 2020-11-27 12:49

Why should i do something like this:

inline double square (double x) { return x*x;}

instead of

double square (double x) {          


        
相关标签:
6条回答
  • 2020-11-27 13:14

    Yes there is a difference. https://isocpp.org/wiki/faq/inline-functions.

    When you specify that a function is inline you are causing the compiler to put the code of the method in where ever it is being called.

    void myfunc() {
      square(2);
    }
    

    is identical to

    void myfunc() {
       2 * 2;
    }
    

    Calling a function is good for code clarity, but when that function is called the local state has to be pushed to the stack, a new local state is setup for the method, and when it is done the previous state needs to be popped. That is a lot of overhead.

    Now if you up your optimization level, the compiler will make decisions like unrolling loops or inlining functions. The compiler is still free to ignore the inline statement.

    0 讨论(0)
  • 2020-11-27 13:15

    From Wikipedia: Inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.

    http://en.wikipedia.org/wiki/Inline_function

    0 讨论(0)
  • 2020-11-27 13:17

    The inline function, if the compiler complies, will include the inline function in the code in which it was called as if no function was called (as though you had put the logic in the calling function) and avoid the function call overhead.

    0 讨论(0)
  • 2020-11-27 13:20

    inline works well with the concept of procedural abstraction:

    inline double square (double x) { return x*x;}
    
    int squareTwice(double x) {
        double first = square(x);
        double second = square(x);
        return first * second; 
    }
    

    The above is fundamentally similar to the following:

    int squareTwice(double x) {
        double first = x*x;
        double second = x*x;
        return first * second; 
    }
    

    This happens because when the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream; therefore, it may be easier to procedurally abstract the second example to the first example.

    Procedural abstraction makes it possible to break up a routine into smaller subroutines that are much easier to read (although this can be a style choice).

    0 讨论(0)
  • 2020-11-27 13:31

    On a modern compiler there is likely not much difference. It may be inlined without the inline and it may not be inlined with the inline.

    0 讨论(0)
  • 2020-11-27 13:33

    The former (using inline) allows you to put that function in a header file, where it can be included in multiple source files. Using inline makes the identifier in file scope, much like declaring it static. Without using inline, you would get a multiple symbol definition error from the linker.

    Of course, this is in addition to the hint to the compiler that the function should be compiled inline into where it is used (avoiding a function call overhead). The compiler is not required to act upon the inline hint.

    0 讨论(0)
提交回复
热议问题