template specialization overriding non-specialization?

J

Joseph Turian

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-lite/templates.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,
 
T

Thomas Tutone

Joseph said:
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-lite/templates.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-lite/templates.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
 
A

andy

Joseph said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top