Home » C++ » c++ inline function?

c++ inline function?

Posted by: admin November 30, 2017 Leave a comment

Questions:

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?

Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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