linking instanciated inline functions

S

Sylvain Audi

I know that declaring a function as inline does not imply the function
will actually be inlined (e.g. recursive functions, etc...).

In that case, where would the function be instanciated?
My guess is that it would be instanciated in the compilation unit that
uses it. But in this case, several compilation units that use the same
inline function would mean that the linker has to deal with several
instances of the same function: shouldn't the linker fail on this?

So:
- are the linkers required to "optimize" and remove duplicate instances
of a function?
- do compilation units that require instanciating an inline function do
it only locally, not exposing the instance to the linker?

I actually never faced any such problem, as my compiler/linker handles
it somehow, but I was simply wondering what was going on really...
 
I

Ian Collins

Sylvain said:
I know that declaring a function as inline does not imply the function
will actually be inlined (e.g. recursive functions, etc...).

In that case, where would the function be instanciated?
My guess is that it would be instanciated in the compilation unit that
uses it. But in this case, several compilation units that use the same
inline function would mean that the linker has to deal with several
instances of the same function: shouldn't the linker fail on this?

So:
- are the linkers required to "optimize" and remove duplicate instances
of a function?

The C++ Standard requires at most one copy in the entire program of a
non-static inline function, so in effect, yes.
- do compilation units that require instanciating an inline function do
it only locally, not exposing the instance to the linker?
Some compilers may do this to improve build times, but it isn't standard
conforming. I tend to default to this option with my compiler for
development builds where compiling with debug disables function inlining.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top