3

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?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Luke
  • 65
  • 3
  • The Standard says at `14.7.3/6`: "If a template, a member template or the member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required." – Johannes Schaub - litb Jan 13 '10 at 22:44

4 Answers4

17

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
  • 20,727
  • 18
  • 94
  • 165
  • 2
    Thank you: excellent answer. Does this mean that forward-declaring the specialization (in the header file) will suppress the generation of Foo in main.C? – Luke Jan 13 '10 at 16:28
  • Yes, I meant to mention that. Edited my answer for future readers ;) – Joseph Garvin Jan 13 '10 at 16:29
  • 3
    Some may not be aware of the syntax for this. You can do that by writing `template<> int Foo::foo();` - just omit the body. – Johannes Schaub - litb Jan 13 '10 at 22:39
  • It might be good to note that as a result of violating ODR, you can get 7 or 6 depending on circumstances. Frequently you can see in Debug it works as expected, as functions are not inlined, but in Release (with inlining) it breaks. – Suma Aug 04 '11 at 20:19
  • What compilers are you guys using? In vs 2012, no matter targeting vc90toolset or vc110toolset, the program actually cause a linker error, unless the body is omitted in foo.c – Trout.Z Jan 18 '13 at 03:09
  • GCC with default ld still has this problem I think. If you use GCC with whole program compilation or the gold linker I'm not sure though. – Joseph Garvin Jan 18 '13 at 03:36
2

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

Macke
  • 24,812
  • 7
  • 82
  • 118
1

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
  • 36,376
  • 13
  • 83
  • 122
-2

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
  • 596
  • 4
  • 14
  • 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 Jan 13 '10 at 16:23