简体   繁体   中英

c++ inline function?

Why should i do something like this:

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

instead of

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

Is there a difference?

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.

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.

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 .

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

如果编译器遵从,则内联函数将在被调用的代码中包括内联函数,就好像没有调用任何函数一样(就像您已将逻辑放在调用函数中一样),并避免了函数调用开销。

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM