About partially specialize a templated class' member method.
Is it possible to partially specialize a templated class' member method by only write the partially specialized version of the method and without rewrite the partially specilaized version of the whole class?
Rewrite the whole class cause bad taste of my code...sigh.
Any indication is appreciate.Thanks in advance.
[Edited by - kimryo on July 17, 2008 2:22:16 AM]
if i understood correct, this is what you need
class A
{
template <class T>
void foo(T val);
template <>
void foo<int>(int val)
{
cout<<"this is int";
}
template <>
void foo<bool>(bool val)
{
cout<<"this is bool";
}
void some_other_function()
{
}
};
class A
{
template <class T>
void foo(T val);
template <>
void foo<int>(int val)
{
cout<<"this is int";
}
template <>
void foo<bool>(bool val)
{
cout<<"this is bool";
}
void some_other_function()
{
}
};
Quote:Original post by itten
if i understood correct, this is what you need
class A
{
template <class T>
void foo(T val);
template <>
void foo<int>(int val)
{
cout<<"this is int";
}
template <>
void foo<bool>(bool val)
{
cout<<"this is bool";
}
void some_other_function()
{
}
};
Thanks for reply, but this is fully specialize of a templated method of an untemplated class.
Actually what I need is something like this
template<typename T1,typename T2>
class A
{
void foo(T2 val);
void some_other_function();
void And_many_many_other_functions1();
//...
void And_many_many_other_functionsn();
};
template <typename T1,typename T2>
void A<T1,T2>::foo(T2 val)
{
cout<<"this is all templated";
}
template <typename T1>
void A<T1,int>::foo(int val)
{
cout<<"this is partly int";
}
template <typename T1>
void A<T1,bool>::foo(bool val)
{
cout<<"this is partly bool";
}
I wrote like this, but my MSC compiler said it is illegal to do so, declaration and definition mismatch.
Since partially specialized function is also illegal in C++, I can not figure out a way to done like that.
Gcc (v3.4.4) seams able to handle it, and visual studio 8:
#include <iostream>using std::cout;using std::endl;template<class T>class Test {public: void doit() { cout << "General doit!" << endl; }};template<>void Test<int>::doit() { cout << "Specialized doit!" << endl; }int main() { Test<char> a; Test<int> b; a.doit(); b.doit(); return 0;}
i got it. Sorry, as far as i know, there is no way to do like you want. I tried your example in VS2005 and got the error C2244. In its description i found an axample whitch is exactly the same as yours. Here it is:
"
You cannot partially specialize a function template.
Copy Code
// C2244d.cpp
template<class T, class U>
class QRS {
void func(T t, U u);
};
template<class T>
void QRS<T,int>::func(T t, int u) {} // C2244
"
"
You cannot partially specialize a function template.
Copy Code
// C2244d.cpp
template<class T, class U>
class QRS {
void func(T t, U u);
};
template<class T>
void QRS<T,int>::func(T t, int u) {} // C2244
"
mzeo77: That's still complete, not partial, specialization.
kimryo: Functions (member or otherwise) can't be partially specialized.
kimryo: Functions (member or otherwise) can't be partially specialized.
Quote:Original post by mzeo77
Gcc (v3.4.4) seams able to handle it, and visual studio 8:#include <iostream>using std::cout;using std::endl;template<class T>class Test {public: void doit() { cout << "General doit!" << endl; }};template<>void Test<int>::doit() { cout << "Specialized doit!" << endl; }int main() { Test<char> a; Test<int> b; a.doit(); b.doit(); return 0;}
For fully specialized, of course can do so. Try make two template parameters, and partially specialize one of the two.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement