C++: Do function wrappers work with inline?
Asked Answered
R

2

7

If you've enabled full optimizations in your compiler and have classes setup like this:

class A
{
    void Do_A_Stuff();
};
class B
{
    A a;
    void Do_B_Stuff() { a.Do_A_Stuff(); }
};
class C
{
    B b;
    void Do_C_Stuff() { b.Do_B_Stuff(); }
};
class D
{
    C c;
    void Do_D_Stuff() { c.Do_C_Stuff(); }
};

Is there ever a situation where calling Do_D_Stuff() would be slower than directly calling Do_A_Stuff()? Also, would this require the inline keyword on each wrapper 'chain' or, since it is only a suggestion, could the compiler decide to optimize this without the keyword?

I realize there is a lot of information about inlining available, but I could not find any information specifically about chaining many wrappers together.

Recorder answered 28/3, 2013 at 23:35 Comment(0)
R
9

Also, would this require the inline keyword on each wrapper 'chain' or, since it is only a suggestion, could the compiler decide to optimize this without the keyword?

Yes, the compiler could decide to optimize it anyway, and it could also decide not to optimize it even if you specified the inline keyword (possibly producing a warning if the appropriate compiler options are set) - notice, that member functions defined in a class definition are implicitly marked as inline.

In general, if inlining is possible, the compiler will decide whether to inline or not based on the body of the function being called. However, inlining may not be possible at all if the function is a virtual function, or if the definition of the function is not visible to the compiler.

Provided that the conditions for inlining are satisfied and that the compiler considers it appropriate, there is no technical problem in inlining over a chain of function calls.

As a minor remark, notice that the functions in your classes should be public, otherwise they won't be accessible to your wrappers.

Recto answered 28/3, 2013 at 23:39 Comment(7)
Yes, the functions should be public, I was going for brevity. :)Recorder
"or if the definition of the function is not visible to the compiler." - does this apply to link time code generation in MSVC (or equivalent in other compilers)? I've always been curious about that.Syndrome
@JBentley: Cross-module inlining is also possible, yes. There is also this relevant Q&A that you can check. Thank you for your remarkRecto
@AndyProwl I just want to clarify one more thing before I mark this as the answer, if ALL of the functions in my example were inlined, would a call to Do_D_Stuff() basically be directly running the code within Do_A_Stuff() once compiled? (I'm trying to figure out if the end result is skipping the 'overhead' of multiple wrappers.) Thanks!Recorder
@Jonathan: Yes, that's what would happenRecto
Virtual functions may be inlinable if the compiler can statically deduce the concrete type of which a member is being invoked.Stempson
@NathanErnst: Indeed. That's why I wrote "may" :)Recto
T
0

The functions are defined inside the class definition, so the inline keyword is implicit in this case.

Turbary answered 28/3, 2013 at 23:38 Comment(2)
It should be noted that even using inline outside the class definition is no guarantee that the compiler will inline your code. It is up to the compiler to decide.Lem
Compilers may inline functions not marked as inline, and not inline functions marked as inline.Alfano

© 2022 - 2024 — McMap. All rights reserved.