why template functions in boost lib are all inlined?

Discussion in 'C++' started by newbie, Apr 24, 2007.

  1. newbie

    newbie Guest

    I happened to read boost library code and realized that most (the part
    I read) functions are inlined like:

    template <class Key>
    inline void Foo(const Key& k) {
    ...
    ...
    }

    Is there a strong reasoning behind this?

    Thanks
     
    newbie, Apr 24, 2007
    #1
    1. Advertising

  2. newbie

    Mark P Guest

    newbie wrote:
    > I happened to read boost library code and realized that most (the part
    > I read) functions are inlined like:
    >
    > template <class Key>
    > inline void Foo(const Key& k) {
    > ...
    > ...
    > }
    >
    > Is there a strong reasoning behind this?
    >
    > Thanks
    >


    It's not just boost. Generally template functions are inlined (often
    implicitly, without the use of the "inline" keyword). Since all of a
    program's template instances are not generally known until all
    translation units have been compiled, the template function definition
    needs to be accessible wherever the function is instantiated. For an
    alternative approach look up the "export" keyword, but know that this
    feature is not widely supported (Comeau, I believe, being the well-known
    exception to this).
     
    Mark P, Apr 25, 2007
    #2
    1. Advertising

  3. On Wed, 25 Apr 2007 00:06:04 GMT, Mark P
    <> wrote:

    >newbie wrote:
    >> I happened to read boost library code and realized that most (the part
    >> I read) functions are inlined like:
    >>
    >> template <class Key>
    >> inline void Foo(const Key& k) {
    >> ...
    >> ...
    >> }
    >>
    >> Is there a strong reasoning behind this?


    The same reasoning that would lead one to inline non-templated functions.

    >>
    >> Thanks
    >>

    >
    >It's not just boost. Generally template functions are inlined (often
    >implicitly, without the use of the "inline" keyword). Since all of a
    >program's template instances are not generally known until all
    >translation units have been compiled, the template function definition
    >needs to be accessible wherever the function is instantiated. For an
    >alternative approach look up the "export" keyword, but know that this
    >feature is not widely supported (Comeau, I believe, being the well-known
    >exception to this).


    If you mean that templated functions are /always/ inlined, then you would be
    mistaken. The compiler/linker is required to gather all instantiations of a
    templated function, and consolidate them into one implementation, which may
    not be inlined.

    If you mean that templated functions are /more likely/ to be automatically
    inlined, then you're probably technically correct. A very small templated
    function would likely be automatically inlined, while a very small
    non-templated function might be impossible to inline because its
    implementation may not be visible to the caller. However, I don't think
    compilers generally favor templated functions for inlining just because
    they're templated. Note that taking the address of the function, etc. will
    still force a non-inlined instance to be created.

    With Whole Program Optimization available on many compilers these days, the
    inlining potential of templated and non-templated functions are probably
    exactly the same.

    -dr
     
    Dave Rahardja, Apr 28, 2007
    #3
    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. Andy
    Replies:
    3
    Views:
    305
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,230
    Smokey Grindel
    Dec 2, 2006
  3. inline functions not inlined

    , Mar 6, 2006, in forum: C Programming
    Replies:
    9
    Views:
    341
    David Resnick
    Mar 7, 2006
  4. sks
    Replies:
    4
    Views:
    321
    Victor Bazarov
    Dec 6, 2005
  5. Michal
    Replies:
    4
    Views:
    359
    Jorgen Grahn
    Nov 22, 2008
Loading...

Share This Page