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