C ++: do function wrappers work with inline?

If you included full optimization in your compiler and configured the classes as follows:

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 a call to Do_D_Stuff() will be slower than a direct call to Do_A_Stuff() ? Also, this requires the inline on each wrapper chain, or since this is just a suggestion, can the compiler decide to optimize this without the keyword?

I understand that there is a lot of information about nesting, but I could not find any information on how to tie many wrappers together.

+6
source share
2 answers

In addition, this will require the inline keyword on each wrapper chain, or since this is just a suggestion, can the compiler decide to optimize this without the keyword?

Yes, the compiler may decide to optimize it anyway, and it may also decide not to optimize it, even if you specify the inline (perhaps by creating a warning if the appropriate compiler options are set) - note that this member of the function is defined in class definitions are implicitly marked as inline .

In general, if embedding is possible, the compiler will decide whether to enable or not based on the body of the called function. However, nesting may not be possible at all if the function is a virtual function or if the function definition is not displayed to the compiler.

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

As a minor note, note that the functions in your classes must be public , otherwise they will not be available to your wrappers.

+7
source

Functions are defined inside the class definition, so the inline implicit in this case.

0
source

All Articles