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 

Is it possible that Template member function of Template cla

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





PostPosted: Thu Dec 11, 2003 10:43 am    Post subject: Is it possible that Template member function of Template cla Reply with 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<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





PostPosted: Fri Dec 12, 2003 2:04 am    Post subject: Re: Is it possible that Template member function of Template Reply with quote



"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
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.