views:

173

answers:

5

Template code is not compiled until the template function is used. But where does it save the compiled code, is it saved in the object file from which used the template function in the first place?

For example, main.cpp is calling a template function from the file test.h, the compiler generates an object file main.o, Is the template function inside the main.o file? because template code is not inlined, is it?

A: 

It is always inlined (meaning, it is always internal linkage, having inline semantics). It may in fact be not inlined after all, just as an inline function, however, template is not code. It is a "template for making code". Therefore, it will normally reside in a header, except special cases, see below.

There was an idea to make something else, codenamed "export keyword". It was removed from standard.

Special cases: you can compile template instantiations into an object file, without having them used. This is the only way to avoid having all template code inlined. This is how it is done:

template class std::vector<MyClass>;

This will force the compiler to instantiate a template in the current location. C++0x will have a syntax to force compiler not to do it, and have the linker search for template instantiation elsewhere:

extern template class std::vector<MyClass>; // C++0x only

Pavel Radzivilovsky
It is not inlined, necessarily. It is instantiated in the compilation unit, and may be inlined but may not.
Michael E
The export keyword had nothing to do with inlining but was instead about providing templates in compiled form for later instantiation by client code.
Noah Roberts
A: 

You mean instantiated, not compiled. At compile time the compiler finds out each and every version that your code uses and instatiates (in object files) all the required versions.

Francesco
+4  A: 

It's totally compiler implementation dependant. Most compilers will generate code around, inline or in cpp-like files and then compile with that. Sometimes, with optimization setup, some compilers will even reuse the same code instead of recreate it for each cpp.

So you have to see your compiler's doc for more details.

Klaim
This is the actual correct answer, not the one that was accepted by the OP.
Noah Roberts
+3  A: 

Yes, the template function code is emitted in the main.o file. Some of it may be inlined, as any other code may be inlined, but in general, code for templates is emitted in any file in which the template is instantiated. Think of it as first instantiating the template code to produce normal non-template functions, and then compiling those functions with whatever inlining and optimization the compiler applies to any other function.

When the same template instantiation (e.g. std::vector<int>) occurs in multiple compilation units (.cpp files), there is a bit of difficulty, because the code is instantiated in each of them. There are various ways of handling this, sometimes involving a cleanup step in the linking phase where duplicate template instantiations are resolved into a single one; your compiler manual can provide more information on exactly how it handles that situation.

Michael E
so creating a template class with a dummy variable that is not used is not the same as creating a class and putting everything in the header file. The later will be inline and the template class is up to the compiler what it does with it?
hidayat
@hidayat: No. The same rules apply for “normal” classes and class templates.
Konrad Rudolph
Re duplicates: I think it's a standard feature of object file formats to have section that can be combined at link time. I *think* at least one system calls them `COMDAT` sections: http://docsun.cites.uiuc.edu/sun_docs/C/solaris_9/SUNWdev/LLM/p44.html (IIRC some linkers will even check if the sections are identical to catch some errors.)
BCS
+1  A: 

Template code is compiled even if it is never instantiated. Otherwise, compilers couldn't be required to emit a diagnostic for this:

template< typename T >
void f()
{
  blah
}

Template compilation happens in two phases. Besides the basic checks, everything that depends on template parameters can only be checked when a template is instantiated and the formal parameters are filled in with actual types. For example, here

template< typename T >
void f()
{
  typename T::nested_type x;
}

T::nested_type can only be checked after the template is instantiated and an actual type is given for T. However, a basic check ("given T::nested_type is a type, is this a valid variable definition?") is performed the moment the compiler encounters the template's definition. (That's why that typename is required, BTW. Depending on T, T::nested_type might just as well be the name of member of T or a static data member - which would make T::nested_type x; a syntax error. So we have to tell the compiler to treat T::nested_type as the name of a type.)

sbi