Any way to print out template typename value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    Any way to print out template typename value?

    I have a simple template method that looks like
    Code:
        template < typename T>
        void WhatEver( void)
        {
             T t;
             // How to print with cout the T value (not the t value)?
        }
    I thought maybe RTTI would work, but that deals with classes with virtual methods. Since T is most likely going to be an int or double, is there any built in C++ way to print out the value of T.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I assume you mean to actually output the typename used by the class in which case the answer is basically no.

    Comment

    • mac11
      Contributor
      • Apr 2007
      • 256

      #3
      It might not be exactly what you want, but gcc provides __PRETTY_FUNCTI ON__ which ends up printing the type of T for you:


      Code:
      #include <iostream>
      #include <string>
      using namespace std;
      
      template <typename T>
      void WhatEver(void)
      {
          T t;
          cout << __PRETTY_FUNCTION__ << endl;
      }
      
      int main()
      {
          WhatEver<int>();
          WhatEver<long>();
          WhatEver<string>();
      
          return 0;
      }
      output:

      void WhatEver() [with T = int]
      void WhatEver() [with T = long int]
      void WhatEver() [with T = std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> >]

      If you aren't using gcc maybe you can look under it's hood to find out how __PRETTY_FUNCTI ON__ works?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This is C++. Use the typeid keyword to see the type of the variable at run time:

        Code:
        template < typename T> 
            void WhatEver( void) 
            { 
                 T t; 
                 // How to print with cout the T value (not the t value)? 
                 cout << typeid(t).name() << endl;    //Your answer
            } 
        int main()
        {
            WhatEver<int>();
            WhatEver<double>();
        }
        Research C++ RTTI for further info.

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          This works fine, but the output is not very recognizable. With this format, typeid gives you a name of i for int, j for unsigned int, c for char, and (oh yes) h for unsigned char.

          GNU g++ uses the Itaniium C++ ABI (http://www.codesourcery.com/public/cxx-abi/abi.html). Go past the mangled name section for more info about the encodings.

          It appears to be a format designed to print out any variable defined in the know universe. Whew!

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The name() method is implementation dependent. The idea is that youonly use it for debugging. Otherwise, you are supposed to compare the type_info object returned by typeid with one created from a known type. The removes the dependence on the implementation.

            Code:
            if ( typeid(int) == typeid(myVar) )
            {
                 //etc...
            }
            BTW: Visual Studio typeid name() method returns int for int unsigned char for unsigned char, etc.

            Comment

            Working...