4

for the following function:

inline void A() {
   ...
   B();
   ...
}

is B also inlined by the compiler?

If not, should I make B inline to increase performance?

0

4 Answers 4

6

In most situations, you can rest comfortably leaving inlining-decisions to the compiler, which will know far better than you when and when not it will result in better performance.

In this specific situation, I would stronlgy suspect that it is completely up to the compiler what to do.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, the inline keyword is only a hint or a wish, the compiler is not required to follow it. I would bet that modern compilers simply ignore it.
Depends on the language. C99's inline is not a hint at all..
5

No, the inline keywords will only cause the A code to be inlined inside the caller code. This will not affect inlining of B.

Besides this, in c++, the inline keyword is only a hint to the compiler, which is allowed to ignore it. Modern compilers decide when the functions need to be inlined, even if the keyword is not used.

Comments

4

Somehow all the commenters failed to mention that there are cases when inline is NOT a mere hint to the compiler, but a mandated keyword. It happens when one puts a non-template definition of a function in the header file included by multiple .cpp files. In this case absence of inline will trigger a linker error. As a matter of fact, this is the only case when one should even bother typing those 6 characters. In other cases, compilers will inline everything they can - regardless of this keyword presence.

2 Comments

in the case you mentioned, is the function actually inlined or inline just resolve the linker error?
@MonsterHunter, it is guranteed to resolve a linker error. It does not guaratee to perform the actual inlining. But don't you worry. Modern compilers LOVE inlining. If anything can be inlined, it would be inlined :)
2

When you declare a function/method inline, it is just a hint to the compiler that this particular function should be inlined. The compiler then may or may not inline it. The same applies for nested calls like the call to B() inside A().

I would probably add inline specifier to both of the functions, but that's just a matter of style - modern optimizing compiler like GCC will optimize it anyways.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.