472,958 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Is finding definitions of explicitly specialized template functionscompiler-defined?

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
1 2141
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ruben Campos | last post by:
I have a template class, and I want to create partially (totally) specialized versions of it, by adding new methods to the original base template specification. But it seems that partially...
2
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the...
3
by: BigMan | last post by:
Why cannot I define a member of an explicitly specialized class template out of the class template specialization: template< int i > struct a { static void Do( ); }; template< >
1
by: jn | last post by:
Hi everyone, I was trying to implement a design when I've found what seems a limitation of standard C++. I can't specialize an inner class in this way: template <class ARG> template <>...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
14
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
2
by: Aarti | last post by:
Say I have a class template as follows template<typename T> class foo { public: void test(); }; template<typename T>
16
by: Hendrik Schober | last post by:
Hi, suppose we have template< typename T > struct X; and some specializations: template<>
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.