Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Is finding defi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 44330 of 48044
Post > Topic >>

Is finding definitions of explicitly specialized template functions

by "jason.cipriani@[EMAIL PROTECTED] " <jason.cipriani@[EMAIL PROTECTED] > Apr 2, 2008 at 09:03 AM

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
 




 4 Posts in Topic:
Is finding definitions of explicitly specialized template functi
"jason.cipriani@[EMA  2008-04-02 09:03:39 
Re: Is finding definitions of explicitly specialized template
Greg Herlihy <greghe@[  2008-04-02 20:19:03 
Re: Is finding definitions of explicitly specialized template
James Kanze <james.kan  2008-04-03 05:22:05 
Re: Is finding definitions of explicitly specialized template
"jason.cipriani@[EMA  2008-04-04 08:20:28 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 22:34:30 CDT 2008.