开发者

How does the compiler know to use a template specialization instead of its own instantiation?

开发者 https://www.devze.com 2022-12-16 11:49 出处:网络
Consider the following files: Foo.H template <typename T> struct Foo { int foo(); }; template <typename T>

Consider the following files:

Foo.H

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

template <typename T>
int Foo<T>::foo()
{
  retur开发者_开发问答n 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?


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.


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


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().


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号