Template code inline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Template code inline

    Hi all,

    Is template code "always" inlined???
    Or will compilers instantiate template functions separately if they
    are large?

    Also, what happens to the code if explicit instantiation mechanism is
    used? ]
    Are template functions, objects and member functions then placed as
    separate entities within the object file, as with any non templated
    functions.???
  • tom_usenet

    #2
    Re: Template code inline

    On 14 Nov 2003 03:17:34 -0800, me@stevejpurves .info (Steve) wrote:
    [color=blue]
    >Hi all,
    >
    >Is template code "always" inlined???[/color]

    No. It might be if you define the functions inside the class
    definition, since that implicitly inlines the code.
    [color=blue]
    >Or will compilers instantiate template functions separately if they
    >are large?[/color]

    Yes. The function may be instantiated in multiple translation units
    and the code used in a number of places, but compilers employ a number
    of techniques to ensure that you only end up with a single copy of the
    code in your exe (instantiation databases and linker based
    instantiation merging are two techniques). Of course, the
    implementation may choose to inline the code anyway.
    [color=blue]
    >Also, what happens to the code if explicit instantiation mechanism is
    >used? ][/color]

    You control the translation unit that gets the instantiation, and
    avoid having the template implicitly instantiated in multiple
    translation units.
    [color=blue]
    >Are template functions, objects and member functions then placed as
    >separate entities within the object file, as with any non templated
    >functions.?? ?[/color]

    Yes. Templates are never compiled to object code, only template
    specializations . These can be explicit specializations , implicit
    instatiations or explicit instantiations. This is independent of
    inlining.

    Tom

    Comment

    • Rolf Magnus

      #3
      Re: Template code inline

      Steve wrote:
      [color=blue]
      > Hi all,
      >
      > Is template code "always" inlined???[/color]

      Templates don't have any special connections to inline functions.
      [color=blue]
      > Or will compilers instantiate template functions separately if they
      > are large?[/color]

      They can do that and probably will. There are even cases in which it's
      impossible to inline a function (think recursion or calling the
      function through a pointer)
      [color=blue]
      > Also, what happens to the code if explicit instantiation mechanism is
      > used? ][/color]

      Code for the template instance is generated.
      [color=blue]
      > Are template functions, objects and member functions then placed as
      > separate entities within the object file, as with any non templated
      > functions.???[/color]

      Yes.

      Comment

      Working...