0

Suppose we have a class member function that calls a free function:

class MyClass
{
public:
  void func1() {func2();}
};

I'm curious to know if there is a possibility that the compiler will inline the function inside (func2) because func1 is inline?

Mo Sanei
  • 445
  • 6
  • 22
  • 2
    The compiler doesn't even necessarily inline `func1`. C++ `inline` (whether explicit or implicit) has virtually nothing to do with the optimization known as inlining. –  Jun 04 '13 at 13:59
  • 1
    Marking function as `inline` is not about inlining, but rather about linking rules. – Lol4t0 Jun 04 '13 at 14:00

5 Answers5

2

The fact func2 is inlined or not depends on how complex is func2. The fact func1 is inlined or not depends on how complex it will be after f2 expansion (if any).

inlining is essentially a compiler optimization that does not depend on the fact a function definition is inlined-in-source (hence repeatble) or not.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
1

There's absolutely no basis and no reason for such connection to exist. The matter of inlining the inner function call is independent from the matter of inlining the enclosing function calls.

Addressing your question: the possibility that the inner call will be inlined is there, of course, but again, it is in no way related to whether the outer function is inlined or not.

It should be noted that compiler's decision-making algorithm that decide whether to inline functions will generally be smart enough to avoid excessive code bloat, i.e. to avoid generation of exceedingly long functions. This means that in some cases inlining the outer function might mean that the inner calls will not be inlined. And vice versa.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

Depends on the compiler, and the circumstances.

Compilers decide to inline functions based on:

  1. Visibility of the code. Obviously, it can't inline a function that the compiler has no source code for [1].
  2. Size of the code. Large functions are less likely to be inlined than small functions.
  3. The number of calls to a function. If it's a small function, it may be called hundreds of times and still be inlined, a large function called more than once is probably NOT inlined. At least GCC has a special case for "function only called once" - where it inlines even huge functions under that special case.

So, in this case, if func2 is visible to the compiler, and is small enough that the compiler sees it OK to inline, then it probably will be.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

No, having func1 inline will not automatically cause func2 to be inlined; but there is always a possibility that the compiler will inline func2, independently of whether func1 is inline or not.

gx_
  • 4,690
  • 24
  • 31
0

Inlining behavior is completely implementation defined. The compiler is free to ignore or respect or to use its own heuristics to determine what is should do.

Sometimes compilers do it when not asked: When do compilers inline C++ code?

Sometimes compilers ignore it: https://softwareengineering.stackexchange.com/questions/35432/inline-functions-in-c-whats-the-point

Community
  • 1
  • 1
Sqeaky
  • 1,876
  • 3
  • 21
  • 40