views:

181

answers:

4

Consider the following files:

Foo.H

template <typename T>
struct Foo
{
  int foo();
};

template <typename T>
int Foo<T>::foo()
{
  return 6;
}

Foo.C

#include "Foo.H"

template <>
int Foo<int>::foo()
{
  return 7;
}

main.C

#include <iostream>
#include "Foo.H"

using namespace std;

int main()
{
  Foo<int> f;
  cout << f.foo() << endl;
  return 0;
}

When I compile and run, 7 is printed. What's going on here? When are templates instantiated? If the compiler does it, how does the compiler know not to instantiate its own version of Foo?

A: 

Templates generate different class for every combination of template parameters. This happens at compile time and that is the reason templates should reside in headers. You a make specialization for the int parameter and the compiler calls Foo<int>::foo() for your variable f. It is like overriding virtual function but at compile time.

tony.ganchev
Although your answer correctly describes template behavior, it is not the answer to the question. The compiler shouldn't know about his specialization in Foo.C when it is compiling main.C.
Joseph Garvin
A: 

I'd guess that the compiler instantiates Foo, but then at linking it chooses your specialized Foo instead.

Marcus Lindblom
-1? This is a short version of the actually accepted answer.
Marcus Lindblom
+11  A: 

The issue is that you've violated the one definition rule. In main.C, you've included Foo.H but not Foo.C (which makes sense since it's a source file). When main.C is compiled, the compiler doesn't know that you've specialized the template in Foo.C, so it uses the generic version (that returns 6) and compiles a Foo class. Then when it compiles Foo.C, it sees a full specialization which it can compile right away -- it doesn't need to wait for it to be instantiated somewhere because all the types are filled in (if you had two template parameters and only specialized one this wouldn't be the case), and it compiles a new and distinct Foo class.

Normally, multiple definitions for the same thing cause a linker error. But template instantiations are "weak symbols", which means that multiple definitions are allowed. The linker assumes all definitions are really the same and then picks one at random (well, probably consistently the first one or the last one, but only as a coincidence of the implementation).

Why make them weak symbols? Because Foo might be used in multiple source files, each of which is compiled individually, and each time Foo is used in a compilation unit a new instantiation is generated. Normally, these are redundant, so it makes sense to throw them away. But you've violated this assumption, by providing a specialization in one compilation unit (foo.C) but not the other (main.C).

If you declare the template specialization in Foo.H, then when main.C is compiled it not generate an instantiation of Foo, thus making sure only one definition exists in your program.

Joseph Garvin
Thank you: excellent answer. Does this mean that forward-declaring the specialization (in the header file) will suppress the generation of Foo<int> in main.C?
Luke
Yes, I meant to mention that. Edited my answer for future readers ;)
Joseph Garvin
Some may not be aware of the syntax for this. You can do that by writing `template<> int Foo<int>::foo();` - just omit the body.
Johannes Schaub - litb
A: 

When compiling main.c, the compiler does not know about your specialization. I guess it must generate its own version of Foo<int>::foo() based on the non-specialized template.

But then, when linking, the linker sees that a specialization for Foo<int>::foo() exists. Therefore, it puts in the executable the specialized version.

In the end, even if it does not known it at compile time, main.c will call the specialized version of Foo<int>::foo().

Didier Trosset