473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 2167
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'
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.