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

template specialization overriding non-specialization?

P: n/a
I'm posting this question for a friend who lacks USENET access.
He and I were discussing this question and could not figure out the
solution.

Thank you for your help

Joseph

===

I have a template class with a bunch of methods that are used for most
of the possible template parameter types. However, for a couple of
types the implementation needs to be different. The solutions for
this given on
http://www.parashift.com/c++-faq-lit....html#faq-35.7 don't
seem to work. If I include the instantiations at the bottom of the
definitions (.cpp) file, the compiler seems not to notice the
specialization (in a separate .cpp file). If I include that other
..cpp file in the first one, the compiler complains about multiple
instantiations. Anybody have any suggestions or working examples of
this? I'm using gcc-4 on red hat linux.

TIA,

Apr 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Joseph Turian wrote:
I have a template class with a bunch of methods that are used for most
of the possible template parameter types. However, for a couple of
types the implementation needs to be different. The solutions for
this given on
http://www.parashift.com/c++-faq-lit....html#faq-35.7 don't
seem to work. If I include the instantiations at the bottom of the
definitions (.cpp) file, the compiler seems not to notice the
specialization (in a separate .cpp file). If I include that other
.cpp file in the first one, the compiler complains about multiple
instantiations. Anybody have any suggestions or working examples of
this? I'm using gcc-4 on red hat linux.


The reason for your problem is explained in FAQ 35.12:

http://www.parashift.com/c++-faq-lit...html#faq-35.12

The most straight-forward solution is to put both the template class
definition and all the specializations in one or more header files - do
not put any template definitions or specializations in a cpp file. If
you use the template or any specializations, make sure that cpp file
includes the appropriate header files prior to the use. Do not (in my
view, ever) include one cpp file in another - put it in the header
files.

If you want anything more specific, you're going to need to post code
that illustrates your problem. If you do so, keep it short and make
sure it is compilable (no ellipses or "[my code here"] - so that we can
copy it and compile it ourselves to see your problem.

Best regards,

Tom

Apr 16 '06 #2

P: n/a
Joseph Turian wrote:
Anybody have any suggestions or working examples of
this? I'm using gcc-4 on red hat linux.


As Thomas Tutone says it would help to post a minimal example of what
you are trying to do. It sounds like you want to specialise a member
function for one or more members without inlining the function. In the
header file declare but dont define the function:

// "test.hpp"
#ifndef TEST_HPP_INCLUDED
#define TEST_HPP_INCLUDED
template <typename T>
struct S{
void func();
};

#endif
//--------------------------

Then in a .cpp file do the definitions inc required specialisations
Followed by instantiation requests for any templates you need to use

// "test1.cpp"
#include "test.hpp"
#include <iostream>

template <typename T>
void S<T>::func(){
std::cout << "unspecialised\n";
}

template <>
void S<int>::func()
{
std::cout << "specialised\n";
}

// request instantiations AFTER showing the definitions
// and any specialisations
template
void S<int>::func();
template
void S<double>::func();

//--------------------------------

// link the object code from test1.cpp in your project

// check it works...

// "main.cpp"

#include "test.hpp"

int main()
{
S<double> s_double;
s_double.func();

S<int> s_int;
s_int.func();
}

//---------------------------

// should output:
unspecialised
specialised

HTH

regards
Andy Little

Apr 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.