Template code inline

S

Steve

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.???
 
T

tom_usenet

Hi all,

Is template code "always" inlined???

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

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.
Also, what happens to the code if explicit instantiation mechanism is
used? ]

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

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
 
R

Rolf Magnus

Steve said:
Hi all,

Is template code "always" inlined???

Templates don't have any special connections to inline functions.
Or will compilers instantiate template functions separately if they
are large?

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)
Also, what happens to the code if explicit instantiation mechanism is
used? ]

Code for the template instance is generated.
Are template functions, objects and member functions then placed as
separate entities within the object file, as with any non templated
functions.???

Yes.
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top