31

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.

8 Answers 8

34

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.

17
  • Ahh...so it's kinda of like...when using templates, the compiler sort of "writes the function" for the specific type so that I don't have to do it myself..and that's what it means by generate? Although I kinda don't get why this means that the declarations of the functions should be included in the header files – Nov 5, 2013 at 20:38
  • ...ahh, I don't know what I'm not seeing but I still just don't get it. 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? @g-makulik Nov 5, 2013 at 21:10
  • I know it's been a few days but for some reason I just really really do not understand your explanation. It seems that a really good understanding of how the compiler works is needed for it, or I just don't know why it's not sinking in. Thanks for trying though, hopefully someday all of this makes sense lol @g-makulik Nov 8, 2013 at 1:51
  • I assume the compiler is smart enough to generate code only once per mentioned instance? e.g. if I mention Foo<int> in FileA.cpp and FileB.cpp, it will only generate the code once, correct?
    – vexe
    May 26, 2017 at 6:10
  • @vexe Sure. Your assumption is correct.Was my answer somehow unclear about that? May 26, 2017 at 6:12
24

A template is a pattern for creating code. When the compiler sees the definition of a template it makes notes about that pattern. When it sees a use of that template it digs out its notes, figures out how to apply the pattern at the point where it's being used, and generates code according to the pattern.

4
  • This is the most vivid explanation!
    – James LT
    Nov 28, 2017 at 22:13
  • What is the behavior when the template is not instantiated anywhere in the code? Sep 21, 2023 at 10:41
  • @AnimeshKumar — there’s nothing to do, so the compiler does nothing. Sep 21, 2023 at 12:56
  • If I use PCH, will the template instantiation happen during the PCH generating process?
    – nn0p
    Mar 19 at 3:55
5

What is the compiler suppose to do when it sees a template? Generate all the machine code for all possible data types - ints, doubles, float, strings, ... Could take a lot of time. Or just be a little lazy and generate the machine code for what it requires.

I guess the latter option is the better solution and gets the job done.

2
  • 1
    I think I understand that part a little more now, but I still don't really get why that means that template declarations must be in the header files @Ed Heal Nov 5, 2013 at 20:45
  • The .cpp file needs to know about them. Also the compiler needs to be able to generate the necessary machine code when it comes across the use of them for the appropriate type.
    – Ed Heal
    Nov 5, 2013 at 20:49
3

The main point here is that compiler does not treat a template definition until it meets a certain instance of the template. (Then it can proceed, I guess, like it have a usual class, which is a specific case of the template class, with fixed template parameters.)

The direct answer to your question is: Compiler generates machine code from users c++ code, I think this is wat is meant here by word "generate code".

The template declaration must be in header file because when compiler compiles some source, which use template it HAVE only header file (included in source with #include macro), but it NEED whole template definition. So logical conclusion is that template definition must be in header.

1

When you create a function and compile it, the compiler generates code for it. Many compilers will not generate code for static functions that are not used.

If you create a templated function and nothing uses the template (such as std::sort), the code for the function will not be generated.

Remember, templates are like stencils. The templates tell how to generate a class or function using the given template parameters. If the stencil is not used, nothing is generated.

Consider also that the compiler doesn't know how to implement or use the template until it sees all the template parameters resolved.

0

Your C++ is read by the compiler and turned into assembly code, before being turned in machine code.

Templates are designed to allow generic programming. If your code doesn't use your template at all, the compiler won't generate the assembly code associated. The more data types you associate your template with in your program, the more assembly code it will generate.

0

It won't straight away generate code. Only generates the class or template code when it comes across an instantiation of that template. That is, if you are actually creating an object of that template definition.

In essence, templates allow you abstract away from types. If you need two instantiations of the template class for example for an int and a double the compiler will literally create two of these classes for you when you need them. That is what makes templates so powerful.

-1

10 years late, but for moderne C++ peoples, that need template multifile compilations :

Not sure if it will work with this .h, but I am sure that you don't need it : def.hpp

#pragma once

template <typename T>     // definition is not realy needed
T f(T a);                 // but why not for clarity

void f0();
void f1();

templates.cpp

#include "def.hpp"

static template <typename T>
T f(T a) {
   return a * a + a;
}

f0.cpp

#include "def.hpp"

#include "templates.cpp" // < -------

void f0() {
   printf("f0 = %f\n", f<float>(7.0));
}

f1.cpp

#include "def.hpp"

#include "templates.cpp" // < -------

void f1() {
   printf("f1 = %i\n", f<int>(-9));
}

main.cpp

#include "def.hpp"

#include "templates.cpp" // < -------

int main() {
    f0();
    f1();
    printf("main = %f\n", f<double>(5.7));
}

Compilation

No need to compile templates.cpp as it will be just copy-past to other .cpp files

g++ f0.cpp f1.cpp main.cpp -o main -lm

Or

g++ -c f0.cpp -lm

g++ -c f1.cpp -lm

g++ -c main.cpp -lm

g++ *.o -o main -lm

rm *.o

6
  • You should use a different extension, not .cpp (having a .cpp that's not compiled directly is confusing). You should include it at the bottom of def.hpp, not manually. Nov 30, 2023 at 12:53
  • @HolyBlackCat ? i never heard that .cpp is not meant to be .o compiled.
    – K V
    Nov 30, 2023 at 13:00
  • @HolyBlackCat I do not code in c++. I code in C, and use nvidia nvcc to compile cuda code for gpu, that use c++. So in C, you do .h and .c, that's all. And for faster compilation you do some gcc -c file.c & and then waitso everything is threaded throught cpu cores. So idk, for me it's 100% logic to compile like this. Tell me what are standarts today for C++ librarys
    – K V
    Nov 30, 2023 at 13:02
  • "i never heard that .cpp is not meant to be .o compiled" That's not what I said. What I'm saying is that you create templates.cpp, but don't pass it to g++. If it's not intended to be processed directly by GCC, it should have a different extension. Nov 30, 2023 at 13:08
  • ah ok, but I don't know any other extentions. Maybe you mean I could name it "templates.template" or just "templates".
    – K V
    Nov 30, 2023 at 13:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.