Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template specialization overriding non-specialization?

Reply
Thread Tools

template specialization overriding non-specialization?

 
 
Joseph Turian
Guest
Posts: n/a
 
      04-16-2006
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,

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      04-16-2006

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

 
Reply With Quote
 
 
 
 
andy@servocomm.freeserve.co.uk
Guest
Posts: n/a
 
      04-16-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
Template specialization for templated and primitive type template parameters case2005 C++ 3 02-13-2005 06:53 PM
template specialization of a template class pit3k C++ 8 02-09-2005 02:39 AM
Template Specialization, subclassing and overriding Massimiliano Alberti C++ 8 02-07-2004 06:39 PM
Specialization of a template within template David B. Held C++ 2 10-26-2003 11:59 AM



Advertisments