By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,009 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,009 IT Pros & Developers. It's quick & easy.

Is finding definitions of explicitly specialized template functionscompiler-defined?

P: n/a
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 <intstruct A {
A () { cout << "default" << endl; }
};

template <intvoid f ();
==== main.cpp ====

#include "A.h"

template <intvoid f () { cout << "default" << endl; }

int main () {
A<0x;
A<1y;
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
Apr 2 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Apr 2, 9:03*am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
==== A.h ====

#include <iostream>
using std::cout;
using std::endl;

template <intstruct A {
* A () { cout << "default" << endl; }

};

template <intvoid f ();

==== main.cpp ====

#include "A.h"

template <intvoid f () { cout << "default" << endl; }

int main () {
* A<0x;
* A<1y;
* 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<1has an explicit specialization

The forward declaration of A<1now 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

Apr 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.