Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Class Template Full and Partial Specialization query

C++ Class Template Full and Partial Specialization query

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasewpfhelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ComplexLifeForm
    wrote on last edited by
    #1

    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 :) :)

    S 1 Reply Last reply
    0
    • C ComplexLifeForm

      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 :) :)

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      #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?

      C 1 Reply Last reply
      0
      • S Stuart Dootson

        #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?

        C Offline
        C Offline
        ComplexLifeForm
        wrote on last edited by
        #3

        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 :) :)

        S 1 Reply Last reply
        0
        • C ComplexLifeForm

          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 :) :)

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          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.

          C 1 Reply Last reply
          0
          • S Stuart Dootson

            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.

            C Offline
            C Offline
            ComplexLifeForm
            wrote on last edited by
            #5

            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 :) :)

            S 1 Reply Last reply
            0
            • C ComplexLifeForm

              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 :) :)

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              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++
              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups