Are functions called within inline functions forced inline as well?

clickynext

Platinum Member
Dec 24, 2004
2,583
0
0
//FILE1.cpp:
extern"C" void foo()
{
//some code
}

//FILE2:
__forceinline void bar()
{
foo();
}

Does foo exist in only one memory location, or is there a copy of it wherever there is bar?

Thanks!
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Originally posted by: nickbits
foo() will not be made inline in bar()

Rather, it won't be forced to be inlined with bar. If foo is simple enough many compilers will automatically inline simple functions as an optimization.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Cogman
Originally posted by: nickbits
foo() will not be made inline in bar()

Rather, it won't be forced to be inlined with bar. If foo is simple enough many compilers will automatically inline simple functions as an optimization.

Not if the functions are in different files, as suggested by the comments.
 

Venix

Golden Member
Aug 22, 2002
1,084
3
81
Originally posted by: degibson
Originally posted by: Cogman
Originally posted by: nickbits
foo() will not be made inline in bar()

Rather, it won't be forced to be inlined with bar. If foo is simple enough many compilers will automatically inline simple functions as an optimization.

Not if the functions are in different files, as suggested by the comments.

Visual C++ can. Visual Studio 2005 and later have "Whole Program Optimization," which can inline functions that are defined in different compilation units.
 

postmortemIA

Diamond Member
Jul 11, 2006
7,721
40
91
it is often configurable on compiler level. gcc has options howto deal with inline functions.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Re: Inlining functions across files
Originally posted by: Venix
Visual C++ can. Visual Studio 2005 and later have "Whole Program Optimization," which can inline functions that are defined in different compilation units.

Neat.

One last note: In the example, extern "C" forces foo() to have external C-language visibility. Even if you do something exotic with your compiler to force inlining within bar(), foo() still needs to exist as a symbol in the output text. It would be interesting to see how various compilers would react under a constraint of extern "C" inline.
 
Sep 29, 2004
18,656
68
91
In general,m these days you should not inline. Modern compilers should inline things for you. And if you were t oinline to much and it did get inlined, it can actually slow down your code in some cases. Even more reason to let the compiler handle it.

Not to mention that depending on how you are compiling, a modification to something that is inline might not replace it's usage everywhere which can cause bugs. This is more of an argument to never put actually source code in headers though.

Bottom line. Code your code to work and optimize later if it doesn't run on the target hardware fast enough.