 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Hyung Suk Lee Guest
|
Posted: Thu Dec 11, 2003 10:43 am Post subject: Is it possible that Template member function of Template cla |
|
|
It is template member function specialization.
It's possible
class A
{
template<int N>
void On();
};
template<>
void A::On<3> ()
{
}
But ...
template<typename T>
class A
{
template<int N>
void On();
};
template<>
template<>
void A<int>::On<3> ()
{
} //It's possible
template<typename T>
template<>
void A<T>::On<3> ()
{
} //Not compiled but I want
If It is possible , please how~
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Fri Dec 12, 2003 2:04 am Post subject: Re: Is it possible that Template member function of Template |
|
|
"Hyung Suk Lee" <bysju (AT) hotmail (DOT) com> schrieb im Newsbeitrag
news:jKVBb.111605$ln3.2897338 (AT) news (DOT) bora.net...
| Quote: | It is template member function specialization.
It's possible
class A
{
template<int N
void On();
};
template
void A::On<3> ()
{
}
But ...
template<typename T
class A
{
template
void On();
};
template
template
void A
{
} //It's possible
template<typename T
template
void A
{
} //Not compiled but I want
If It is possible , please how~
|
Just go ahead and implement the function bodies right in the header file
where you declare your class. Consider this (very stupid) example:
template<typename T>
class CObj {
public:
template<int Nr>
T Foo() {
return Nr * 2;
};
template<>
T Foo<4>() { return 4; };
};
int main()
{
CObj<int> Obj;
CObj<double> Obj2;
cout << Obj.Foo<3>() << endl;
cout << Obj.Foo<4>() << endl;
cout << Obj2.Foo<4>() << endl;
return 0;
}
HTH
Chris
[ 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
|
|