 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
news Guest
|
Posted: Tue Nov 29, 2005 2:05 pm Post subject: Template class specialisation |
|
|
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
|
Posted: Wed Nov 30, 2005 10:57 am Post subject: Re: Template class specialisation |
|
|
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) (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
|
Posted: Wed Nov 30, 2005 11:00 am Post subject: Re: Template class specialisation |
|
|
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.
"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 |
|
 |
|
|
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
|
|