0

Suppose we fave a following header file:

Header1.h

#include "Header2.h"

class A
{
public:
    void Function();
}

inline void A::Function() 
{ 
    // Code ...
    OtherFunction(); // Function from Header2.h
    // Code ...
}

If a compiler actually chooses to inline the Function what would happen with the call to OtherFunction:

  1. If OtherFunction definition was available for current translation unit, could it also possibly be inlined within the body of Function? (Basically are nested inlines available?)
  2. If OtherFunction definition was not available for current translation unit, will it remain a simple function call, while the code around it gets inlined? (In a situation where compiler chose to inline Function)
Laurynas Lazauskas
  • 2,855
  • 2
  • 21
  • 26

1 Answers1

1

Most of the modern compilers actually inlines functions for you when you build your application in release configuration - The effect of OtherFunction being declared as inlined/normal will be the same as compiler will decide which is the better option. I certainly know this hapens with MSVC10 and above compilers (Visual Studio). As a matter of fact, inline whatever you want but the compiler ultimately governs what gets inlined.

An example is that if your function calls are chained in a way that the push and pop operations won't make sense after inline your functions won't be inlined (even if you declare them to be inlined)

If it makes things any easier - see:

When to use inline function and when not to use it?

Benefits of inline functions in C++?

More importantly, How deep do compilers inline functions?

has already got a good explanation.

Community
  • 1
  • 1
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • So even if `OtherFunction` is not marked with `inline` keyword and is defined in separate .cpp file, it still might end up being inlined? – Laurynas Lazauskas Jan 10 '15 at 17:03
  • if you see the links I provided, you can see that `inline` delcarations are merely suggestions to the compiler. Your compiler will ultimately inline what it chooses appropriate. Optimisation is what you are after, more than inlining. – ha9u63a7 Jan 10 '15 at 17:04
  • I feel like a wasn't clear enough with my questions, so I edited them to be more specific. Could you look again please? – Laurynas Lazauskas Jan 10 '15 at 17:27
  • 1
    @LaurynasLazauskas Have you read [this](http://stackoverflow.com/questions/7463034/how-deep-do-compilers-inline-functions)? – ha9u63a7 Jan 10 '15 at 18:53