Re: Is finding definitions of explicitly specialized template
functions compiler-defined?
On Apr 2, 9:03 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote:
==== A.h ====
#include <iostream>
using std::cout;
using std::endl;
template <int> struct A {
A () { cout << "default" << endl; }
};
template <int> void f ();
==== main.cpp ====
#include "A.h"
template <int> void f () { cout << "default" << endl; }
int main () {
A<0> x;
A<1> y;
f<0>();
f<1>();
return 0;
}
==== spec.cpp ====
#include "A.h"
template<> A<1>::A () { cout << "A 1!" << endl; }
template<> void f<1> () { cout << "f 1!" << endl; }
==== END EXAMPLE ====
When using GCC:
g++ main.cpp spec.cpp
The linker seems to know that since explicit specializations of
certain functions are present in other object files, it should use
them instead of the default implementations, and the output is:
default
A 1!
default
f 1!
I have not tried this with any other compilers besides GCC 4.1.2. My
question is: Is the behavior of the linker here specific to GCC's
linker?
The fact that the program works as expected is simply a lucky
accident. After all, the program instantiates two different A
constructors - one from the general template and the other from an
explicit specialization. Note that having two different definitions
for the same function violates C++'s One Definition Rule (ODR), so the
program has no expected, defined behavior. Now, in this case, the
linker had a 50/50 chance of choosing the "right" A constructor - and
(luckily or perhaps unluckily, depending on your pointer of view) the
linker made the right choice. Whether the linker will always make the
right choice - and do so even as the number of source files increase
and the odds correspondingly deteriorate - is obviously something of a
gamble.
Or is it always guaranteed that if one object file contains
definitions for explicit specializations of template functions, and
that object file is linked, then they will be used everywhere?
No. The C++ programmer has to ensure that instantiating a template
always use an explicit specialization whenever an explicit
specialization of that template - has been defined somewhere in the
program. And the proper way to prevent the C++ compiler from
instantiating a template from the general template definition, is to
add a forward declaration of the explicit specialization in the
appropriate header file:
// A.h
template <int>
struct A
{
A () { cout << "default" << endl; }
};
template<> struct A<1>; // A<1> has an explicit specialization
The forward declaration of A<1> now inhibits the instantiation of A<1>
from A's general template definition. So, implicit with this forward
declaration, is the promise that an explicit specialization of A<1>
does in fact exist in one of the program's source files. Otherwise, if
the program instantiates A<1>, but the linker cannot find an A<1>
explicit specialization in any of the program's compiled sources
files, then the program will fail to link.
The
biggest reason that I'm unsure is the linker has to do a small amount
of magic to make this work; falling back on the default implementation
if no explicit specializations were found, so I'm wondering if that
magic is defined by C++ or is a GCC implementation detail.
The "magic" in this case was really just a measure of your own good
luck - and not so much a reflection of the gcc linker's magical
powers.
Greg