Is finding definitions of explicitly specialized template functionscompiler-defined?

J

jason.cipriani

Here is an example with 3 files, containing a template structure and
also a template function. The header A.h declares a template structure
A with a default (i.e. for any template parameter), inlined
constructor implementation, and also declares a template function f.
The file main.cpp contains some test code and also the default
implementation for f(). The file spec.cpp contains some explicitly
specialized stuff:

==== 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, if I only compile main.cpp:

g++ main.cpp

Running the program produces the output:

default
default
default
default

Which makes sense. The default A::A() and f() implementations print
the word "default". If I simply add spec.cpp:

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? 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? 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.

Thanks, hopefully this question was clear,
Jason
 
G

Greg Herlihy

==== 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
 
J

James Kanze

Here is an example with 3 files, containing a template
structure and also a template function. The header A.h
declares a template structure A with a default (i.e. for any
template parameter), inlined constructor implementation, and
also declares a template function f. The file main.cpp
contains some test code and also the default implementation
for f(). The file spec.cpp contains some explicitly
specialized stuff:
==== 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, if I only compile main.cpp:
g++ main.cpp
Running the program produces the output:

Which makes sense. The default A::A() and f() implementations
print the word "default". If I simply add spec.cpp:
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? 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? 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.

§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." In other words, undefined
behavior.

FWIW: I think that in main.cpp, g++ instantiates your template,
affecting the instantiation to a weak symbol definition. In
spec.cpp, the specialization generates a normal symbol
definition. The rules of the linker are to first resolve using
a normal symbol definition, if one exists (and more than one is
an error), then try using weak definitions (choosing one more or
less at random if more than one occurs). I think that this is a
relatively common implementation, so there is a fairly good
chance your code will actually work. It's still undefined
behavior, however, and I don't think even g++ actually
guarantees it. In other words, referring to your subject line,
it's not even compiler-defined; it just happens to work that
way, as a side effect of certain implementation choices, which
might change in the next version of the compiler.

The usual solution when explicit specializations are present is
to declare them, either in the header with the template, or in
the case of a specialization on a type argument, in the header
which defines the type.
 
J

jason.cipriani

Great; thanks a lot for the responses, Greg and James. That answers
all my questions. It didn't even occur to me that it was just random
chance, I immediately assumed the linker actually tried to look for
the specializations.


Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top