Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Compilation

Tags:

I'm reading a book about how templates work, and I'm having difficulty understanding this explanation of templates.

It says

When the compiler sees the definition of a template, it does not generate code. It generates code only when we instantiate a specific instance of the template. The fact that code is generated only when we use a template (and not when we define it) affects how we organize our source code and when errors are detected...To generate an instantiation, the compiler needs to have the code that defines a function template or class template member function. As a result, unlike non-template code, headers for templates typically include definitions as well as declarations.

What exactly does it mean by "generate code"? I don't understand what is different when you compile function templates or class templates compared to regular functions or classes.

like image 868
FrostyStraw Avatar asked Nov 05 '13 20:11

FrostyStraw


People also ask

What is a template compiler?

Template compilation requires the C++ compiler to do more than traditional UNIX compilers have done. The C++ compiler must generate object code for template instances on an as-needed basis. It might share template instances among separate compilations using a template repository.

What are templates and how does the compiler interpret them?

A template is a pattern for creating code. When the compiler sees the definition of a template it makes notes about that pattern.

How does C++ compile template?

Instantiation is the process by which a C++ compiler creates a usable function or object from a template. The C++ compiler uses compile-time instantiation, which forces instantiations to occur when the reference to the template is being compiled.

How many times are template classes compiled?

In general, yes, template classes are usually compiled every time they're encountered by the compiler.


1 Answers

The compiler generates the code for the specific types given in the template class instantiation.

If you have for instance a template class declaration as

template<typename T> class Foo { public:      T& bar()      {          return subject;       } private:      T subject; }; 

as soon you have for example the following instantiations

Foo<int> fooInt; Foo<double> fooDouble; 

these will effectively generate the same linkable code as you would have defined classes like

class FooInt { public:      int& bar()      {          return subject;       } private:      int subject; } 

and

class FooDouble { public:      double& bar()      {          return subject;       } private:      double subject; } 

and instantiate the variables like

FooInt fooInt; FooDouble fooDouble; 

Regarding the point that template definitions (don't confuse with declarations regardless of templates) need to be seen with the header (included) files, it's pretty clear why:
The compiler can't generate this code without seeing the definition. It can refer to a matching instantiation that appeared first at linking stage though.

What does a non-template member function have that allows for it to be defined outside of the header that a template function doesn't have?

The declaration of a non-template class/member/function gives a predefined entry point for the linker. The definition can be drawn from a single implementation seen in a compiled object file (== .cpp == compilation unit).
In contrast the declaration of a templated class/member/function might be instantiated from arbitrary compilation units given the same or varying template parameters. The definition for these template parameters need's to be seen at least once. It can be either generic or specialized.

Note that you can specialize template implementations for particular types anyway (included with the header or at a specific compilation unit). If you would provide a specialization for your template class in one of your compilation units, and don't use your template class with types other than specialized, that also should suffice for linking it all together.

I hope this sample helps clarifying what's the difference and efforts done from the compiler.

like image 143
πάντα ῥεῖ Avatar answered Dec 04 '22 04:12

πάντα ῥεῖ