Re: Error when special definition given to template class member function for some specific template parameter
On 12/6/2010 13:21, Daya S. Prasad wrote:
Can we have different definition for template class member function
for special template paramter?
[..]
In the above code we're providing different definition for
TestClass<void>::func().
This is OK, because you can specialize a function template.
But below code doesn't work. It throws
compilation error.
template<class T1, class T2, class T3>
class TestClass {
public:
void func();
};
template<class T1, class T2, class T3>
void TestClass<T1, T2, T3>::func() {
cout<< "Calling for T1, T2, T3"<< endl;
};
template<class T2, class T3>
void TestClass<void, T2, T3>::func() {
cout<< "Calling for void, T2, T3"<< endl;
};
This is an attempt for *partially* specialization of a function template which
is not possible as of today. If you want to realize what you are trying above,
you need to use a class template: Partially specialize the class template and
call a (static) function of that class within the *non*-specialized
TestClass::func().
Roughly:
template<class T1, class T2, class T3>
struct TestFuncHandler {
static void run() {
cout<< "Calling for T1, T2, T3"<< endl;
}
};
template<class T2, class T3>
struct TestFuncHandler<void, T2, T3> {
static void run() {
cout<< "Calling for void, T2, T3"<< endl;
}
};
template<class T1, class T2, class T3>
class TestClass {
public:
void func() { TestFuncHandler<T1, T2, T3>::run(); }
};
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]