C++ Class Template Full and Partial Specialization query
-
Hi, I have template class with the following declaration template class PrintUserData { public: template bool PrintData (T1 const& t1, T2 const& t2); }; How do I specialize this class and it's overload members for the following 1. Specialize for class MyClass / class UserClass etc 2. Overload PrintData template member for class TextFormater i.e. T1 = T2 = TextFormater 3. Overload PrintData template member for class RtfFormater/class TextFormater i.e. T1 = RtfFormatter/TextFormatter, T2 can be anything 4. Overload PrintData template member for class RtfFormater/class TextFormater i.e. T2 = RtfFormatter/TextFormatter, T1 can be anything I find Full Specialization for specific class is easy e.g. template<> class PrintUserData { // other member }; but I am kind of stuck when I tried to oveload the member method I am trying to get comfortable with C++ Templates so please assist me in obtaining the desired result. If someone feels that the template class declartion needs to be changed to achieve the desired result please let me know. I will change it accordingly Thanks and Regards :) :)
-
Hi, I have template class with the following declaration template class PrintUserData { public: template bool PrintData (T1 const& t1, T2 const& t2); }; How do I specialize this class and it's overload members for the following 1. Specialize for class MyClass / class UserClass etc 2. Overload PrintData template member for class TextFormater i.e. T1 = T2 = TextFormater 3. Overload PrintData template member for class RtfFormater/class TextFormater i.e. T1 = RtfFormatter/TextFormatter, T2 can be anything 4. Overload PrintData template member for class RtfFormater/class TextFormater i.e. T2 = RtfFormatter/TextFormatter, T1 can be anything I find Full Specialization for specific class is easy e.g. template<> class PrintUserData { // other member }; but I am kind of stuck when I tried to oveload the member method I am trying to get comfortable with C++ Templates so please assist me in obtaining the desired result. If someone feels that the template class declartion needs to be changed to achieve the desired result please let me know. I will change it accordingly Thanks and Regards :) :)
#include <iostream> template<typename T> class PrintUserData { public: template<typename T1, typename T2> bool PrintData (T1 const& t1, T2 const& t2) { std::cout << "Template" << std::endl; return true; } template<class T2> bool PrintData<int, int>(int const& t1, T2 const& t2) { std::cout << "Partially specialised Template" << std::endl; return true; } bool PrintData(float const& t1, float const& t2) { std::cout << "Function Overload" << std::endl; return true; } }; struct MyClass {}; template<> class PrintUserData<MyClass> { public: template<typename T1, typename T2> bool PrintData (T1 const& t1, T2 const& t2) { std::cout << "MyClass specialisation" << std::endl; return true; } }; int main(int, char**) { PrintUserData<int> pudInt; pudInt.PrintData('a', 'a'); const int a = 10; pudInt.PrintData(a, a); const float b = 10; pudInt.PrintData(b, b); PrintUserData<MyClass> pudMy; pudMy.PrintData('a', 'a'); }
HTH? -
#include <iostream> template<typename T> class PrintUserData { public: template<typename T1, typename T2> bool PrintData (T1 const& t1, T2 const& t2) { std::cout << "Template" << std::endl; return true; } template<class T2> bool PrintData<int, int>(int const& t1, T2 const& t2) { std::cout << "Partially specialised Template" << std::endl; return true; } bool PrintData(float const& t1, float const& t2) { std::cout << "Function Overload" << std::endl; return true; } }; struct MyClass {}; template<> class PrintUserData<MyClass> { public: template<typename T1, typename T2> bool PrintData (T1 const& t1, T2 const& t2) { std::cout << "MyClass specialisation" << std::endl; return true; } }; int main(int, char**) { PrintUserData<int> pudInt; pudInt.PrintData('a', 'a'); const int a = 10; pudInt.PrintData(a, a); const float b = 10; pudInt.PrintData(b, b); PrintUserData<MyClass> pudMy; pudMy.PrintData('a', 'a'); }
HTH?Hi Stuart, Thanks for the help. I tried compiling your code with Visual C++ 2005 Professional SP1 compiler and the compiler gave me the following error :- "error C2768: 'PrintUserData::PrintData' : illegal use of explicit template arguments". "See reference to class template instantiation 'PrintUserData' While the code at (2) compliles properly, the reason could be that the compiler sees t the second declaration/(definition) as a full specialization of member template function for any class. Please correct me if I am wrong template bool PrintData(int const& t1, T2 const& t2)--------------- (1) { std::cout << "Partially specialised Template" << std::endl; return true; } bool PrintData(float const& t1, float const& t2)-------------------- (2) { std::cout << "Function Overload" << std::endl; return true; } On the second thoughts I think there could be an issue with the Visaul C++ 2005 SP1 compiler regarding the template specification implementation Regards :) :)
-
Hi Stuart, Thanks for the help. I tried compiling your code with Visual C++ 2005 Professional SP1 compiler and the compiler gave me the following error :- "error C2768: 'PrintUserData::PrintData' : illegal use of explicit template arguments". "See reference to class template instantiation 'PrintUserData' While the code at (2) compliles properly, the reason could be that the compiler sees t the second declaration/(definition) as a full specialization of member template function for any class. Please correct me if I am wrong template bool PrintData(int const& t1, T2 const& t2)--------------- (1) { std::cout << "Partially specialised Template" << std::endl; return true; } bool PrintData(float const& t1, float const& t2)-------------------- (2) { std::cout << "Function Overload" << std::endl; return true; } On the second thoughts I think there could be an issue with the Visaul C++ 2005 SP1 compiler regarding the template specification implementation Regards :) :)
I compiled it with the VS2008SP1 compiler (that's version 15.00.30729.01 of
cl
) - compiled and ran OK :-) But it doesn't compile with gcc 3.4.5 :( (2) isn't a full specialisation - it's an overload, which is slightly different - it probably alters the relative priority of the overload in partial function ordering. -
I compiled it with the VS2008SP1 compiler (that's version 15.00.30729.01 of
cl
) - compiled and ran OK :-) But it doesn't compile with gcc 3.4.5 :( (2) isn't a full specialisation - it's an overload, which is slightly different - it probably alters the relative priority of the overload in partial function ordering.Hi Stuart, Thanks for letting me know that the real issue is with VS2005 SP1 compiler. I will try to get hold of VS 2008 SP1 compiler and try on it. In last two days I went crazy trying to figure out what is wrong with the declaration/definition, now I know the reason. Do you think VS2008 SP1 compiler is the most complete one when it comes to templates specification implementation? And does it supports export keyword for templates? Thanks and Regards :) :)
-
Hi Stuart, Thanks for letting me know that the real issue is with VS2005 SP1 compiler. I will try to get hold of VS 2008 SP1 compiler and try on it. In last two days I went crazy trying to figure out what is wrong with the declaration/definition, now I know the reason. Do you think VS2008 SP1 compiler is the most complete one when it comes to templates specification implementation? And does it supports export keyword for templates? Thanks and Regards :) :)
psychedelic_fur wrote:
Do you think VS2008 SP1 compiler is the most complete one when it comes to templates specification implementation?
It's not far off the top.
psychedelic_fur wrote:
And does it supports export keyword for templates?
No - but then, which C++ compiler does :-) That's a trick question, 'cause here's the answers:
- Comeau C++[^]
- Intel C++ for Linux..but don't know about Windows.
- Possibly Borland C++