C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template class specialisation

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
news
Guest





PostPosted: Tue Nov 29, 2005 2:05 pm    Post subject: Template class specialisation Reply with quote



Hi all, just a quick template question from a rookie:

I have a template class with lots of functions, many of which are themselves
template functions. (I'm pretty sure this is ok right?) The thing is, I want
to specialise a few of my functions by class template type (ie not by
function template type). I know I could do this by creating a class
specialisation and then duplicating all the rest of the class contents, but
surely this isn't necessary?

Here's some code:

template <class C> class MyClass
{
int iData;
float fEtc;

template <class F> void MyFunction (F param);
}

template <class C> MyClass::template <class F> void (F param)
{
// do stuff
return;
}

template <> MyClass<int>::template <class F> void (F param)
{
// specialisation
return;
}

yay or nay?

=r=



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Wed Nov 30, 2005 10:57 am    Post subject: Re: Template class specialisation Reply with quote



In article <438c3f26$0$22635$c3e8da3 (AT) news (DOT) astraweb.com>, news
<news (AT) news (DOT) astraweb.com> wrote:

Quote:
Hi all, just a quick template question from a rookie:

I have a template class with lots of functions, many of which are themselves
template functions. (I'm pretty sure this is ok right?) The thing is, I want
to specialise a few of my functions by class template type (ie not by
function template type). I know I could do this by creating a class
specialisation and then duplicating all the rest of the class contents, but
surely this isn't necessary?

Yes but you can separate what you want to specialize and derive from

a base class and make it a friend. such as:

#include <iostream>

template <class C,class T>
struct function_base
{
template <class F> void f(F)
{
C *p = static_cast<C *>(this);
std::cout << "general f " << p->x << "n";
}
};

template {
template <class F> void f(F)
{
C *p = static_cast<C *>(this);
std::cout << "specialized f "<< p->x << "n";
}
};

template class MyClass:public function_base<MyClass
{
int x;

public:
MyClass(int a)Mad(a){}
friend struct function_base<MyClass;
};

if MyClass<C> is derived from a single base class then let
function_base derive from it. It MyClass is derived from multiple
base classes derive function_base from them.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Greg Herlihy
Guest





PostPosted: Wed Nov 30, 2005 11:00 am    Post subject: Re: Template class specialisation Reply with quote



news wrote:
Quote:
Hi all, just a quick template question from a rookie:

I have a template class with lots of functions, many of which are themselves
template functions. (I'm pretty sure this is ok right?) The thing is, I want
to specialise a few of my functions by class template type (ie not by
function template type). I know I could do this by creating a class
specialisation and then duplicating all the rest of the class contents, but
surely this isn't necessary?

Here's some code:

template <class C> class MyClass
{
int iData;
float fEtc;

template <class F> void MyFunction (F param);
}

The above class declaration will not compile...

Quote:
template <class C> MyClass::template <class F> void (F param)
{
// do stuff
return;
}

template <> MyClass<int>::template <class F> void (F param)
{
// specialisation
return;
}

....nor will either of these two function declarations.

Quote:
yay or nay?

"nay"

Greg

ps. Although attempting to compile this code with any C++ compiler
would have answered that question better than I just did.

A compiler would probably be less helpful in correcting the errors. So
here are the corrections:

1. Declare MyFunction public in MyClass and add a semicolon to the
class's trailing brace.

2. Declare the general MyClass::F template funciton like so:

template <class C>
template <class F>
void MyClass<C>::MyFunction(F param)
{
std::cout << "in general templaten";
}

.... and the specialized template for int in this way:

template <>
template <class F>
void MyClass<int>::MyFunction(F param)
{
std::cout << "specialized MyClass }

Last, write some sanity-checking test routines for assurance that
nothing is terribly wrong. There should be nothing that would embarrass
its author should the source code ever become public:

int main()
{
MyClass<long> a;
MyClass<int> b;

a.MyFunction(3);
b.MyFunction(4);
}

Program Ouput:
in general template
in specialized MyClass<int> template


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.