linking instanciated inline functions

Discussion in 'C++' started by Sylvain Audi, Jan 13, 2007.

  1. Sylvain Audi

    Sylvain Audi Guest

    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...
    Sylvain Audi, Jan 13, 2007
    #1
    1. Advertising

  2. Sylvain Audi

    Ian Collins Guest

    Sylvain Audi wrote:
    > 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.

    --
    Ian Collins.
    Ian Collins, Jan 13, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    440
  2. Daniel Vallstrom
    Replies:
    2
    Views:
    1,807
    Kevin Bracey
    Nov 21, 2003
  3. Andrew Robinson
    Replies:
    3
    Views:
    295
    Steven Cheng[MSFT]
    Aug 8, 2006
  4. Replies:
    6
    Views:
    335
    Ron Natalie
    Jul 18, 2006
  5. arjor
    Replies:
    0
    Views:
    294
    arjor
    May 22, 2008
Loading...

Share This Page