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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template Question

Template Question

Scheduled Pinned Locked Moved C / C++ / MFC
wpfquestion
6 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi everybody. I have the following question. Is it possible to get behavour like this from templates. template < typename T > class TType { public: CString GetAsString( ) { CString sValue; sValue.Format( GetFormat( ), m_tValue ); return sValue; } protected: T m_tValue; }; Define some function that depended of the type will return different string for formating. template < class int > CString GetFormat( ) { return "%d"; } template < class float > CString GetFormat( ) { return "%.4f"; } and then when this is used TType< int > a; // will use template function for int TType< float > a; // will use template function for float

    M 1 Reply Last reply
    0
    • L Lost User

      Hi everybody. I have the following question. Is it possible to get behavour like this from templates. template < typename T > class TType { public: CString GetAsString( ) { CString sValue; sValue.Format( GetFormat( ), m_tValue ); return sValue; } protected: T m_tValue; }; Define some function that depended of the type will return different string for formating. template < class int > CString GetFormat( ) { return "%d"; } template < class float > CString GetFormat( ) { return "%.4f"; } and then when this is used TType< int > a; // will use template function for int TType< float > a; // will use template function for float

      M Offline
      M Offline
      Martin Vladic
      wrote on last edited by
      #2

      It is invalid C++. Your idea written correctly looks like this: template class TType { public: CString GetAsString() { CString sValue; sValue.Format(GetFormat(), m_tValue); return sValue; } protected: T m_tValue; }; template CString GetFormat(); template <> CString GetFormat() { return "%d"; } template <> CString GetFormat() { return "%.4f"; } Unfortunately, this doesn't work on VC++ 6.0 (GetFormat is called even for float's). Try this: template class TType { public: TType(const T& value) : m_tValue(value) { } CString GetAsString() { CString sValue; sValue.Format(TypeHelper::GetFormat(), m_tValue); return sValue; } protected: T m_tValue; }; template class TypeHelper; template <> class TypeHelper { public: static CString GetFormat() { return "%d"; } }; template <> class TypeHelper { public: static CString GetFormat() { return "%.4f"; } }; -- Martin

      L T 2 Replies Last reply
      0
      • M Martin Vladic

        It is invalid C++. Your idea written correctly looks like this: template class TType { public: CString GetAsString() { CString sValue; sValue.Format(GetFormat(), m_tValue); return sValue; } protected: T m_tValue; }; template CString GetFormat(); template <> CString GetFormat() { return "%d"; } template <> CString GetFormat() { return "%.4f"; } Unfortunately, this doesn't work on VC++ 6.0 (GetFormat is called even for float's). Try this: template class TType { public: TType(const T& value) : m_tValue(value) { } CString GetAsString() { CString sValue; sValue.Format(TypeHelper::GetFormat(), m_tValue); return sValue; } protected: T m_tValue; }; template class TypeHelper; template <> class TypeHelper { public: static CString GetFormat() { return "%d"; } }; template <> class TypeHelper { public: static CString GetFormat() { return "%.4f"; } }; -- Martin

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Are you sure that this will work ?

        M 2 Replies Last reply
        0
        • M Martin Vladic

          It is invalid C++. Your idea written correctly looks like this: template class TType { public: CString GetAsString() { CString sValue; sValue.Format(GetFormat(), m_tValue); return sValue; } protected: T m_tValue; }; template CString GetFormat(); template <> CString GetFormat() { return "%d"; } template <> CString GetFormat() { return "%.4f"; } Unfortunately, this doesn't work on VC++ 6.0 (GetFormat is called even for float's). Try this: template class TType { public: TType(const T& value) : m_tValue(value) { } CString GetAsString() { CString sValue; sValue.Format(TypeHelper::GetFormat(), m_tValue); return sValue; } protected: T m_tValue; }; template class TypeHelper; template <> class TypeHelper { public: static CString GetFormat() { return "%d"; } }; template <> class TypeHelper { public: static CString GetFormat() { return "%.4f"; } }; -- Martin

          T Offline
          T Offline
          Tomasz Sowinski
          wrote on last edited by
          #4

          You need to create a GetFormat function template and specializations for different types. VC++ seems to have problems with some forms of specializations. I was able to make the following code work:

          // in TType template
          CString GetAsString()
          {
          CString sValue;
          sValue.Format( GetFormat(m_tValue), m_tValue );
          return sValue;
          }

          // without this specializations won't compile on VC 6
          template < class T >
          CString GetFormat(const T& unused)
          {
          return CString();
          }

          template < >
          CString GetFormat(const float & unused)
          {
          return "%.4e";
          }

          template < >
          CString GetFormat(const int & unused)
          {
          return "%d";
          }

          Tomasz Sowinski -- http://www.shooltz.com.pl

          1 Reply Last reply
          0
          • L Lost User

            Are you sure that this will work ?

            M Offline
            M Offline
            Martin Vladic
            wrote on last edited by
            #5

            Hm, it seems that discussion boards software stripped some chars from my post. For example, in template specialization definition: template <> class TypeHelper , it stripped "" after class name. Please send me request email, and I will send you correct version of my post. Regards -- Martin

            1 Reply Last reply
            0
            • L Lost User

              Are you sure that this will work ?

              M Offline
              M Offline
              Martin Vladic
              wrote on last edited by
              #6

              I found out why some chars was stripped from my message and how to send it correctly. So, here it is, again: It is invalid C++. Your idea written correctly looks like this: template class TType { public: CString GetAsString() { CString sValue; sValue.Format(GetFormat(), m_tValue); return sValue; } protected: T m_tValue; }; template <class T> CString GetFormat(); template <> CString GetFormat<int>() { return "%d"; } template <> CString GetFormat<float>() { return "%.4f"; } Unfortunately, this doesn't work on VC++ 6.0 (GetFormat<int> is called even for float's). Try this: template <class T> class TType { public: TType(const T& value) : m_tValue(value) { } CString GetAsString() { CString sValue; sValue.Format(TypeHelper::GetFormat(), m_tValue); return sValue; } protected: T m_tValue; }; template <class T> class TypeHelper; template <> class TypeHelper<int> { public: static CString GetFormat() { return "%d"; } }; template <> class TypeHelper<float> { public: static CString GetFormat() { return "%.4f"; } }; -- Martin

              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