9

I am running a code on GCC C++ compiler, to output the type_info::name:

#include <iostream>
#include <typeinfo>

using namespace std;

class shape {
   protected:
   int color;
   public:
   virtual void draw() = 0;
   };


class Circle: public shape {
   protected:
   int color;
   public:
   Circle(int a = 0): color(a) {};
   void draw();
   };

   void Circle::draw() {
   cout<<"color: "<<color<<'\n';
   }

class triangle: public shape {
   protected:
   int color;
   public:
   triangle(int a = 0): color(a) {};
   void draw();
   };

   void triangle::draw() {
   cout<<"color: "<<color<<'\n';
   }

int main() {
   Circle* a;
   triangle* b;
   cout<<typeid(a).name()<<'\n';
   cout<<typeid(b).name()<<'\n';
   }

but I get the following results:

P6Circle
P8triangle

and on demangling,

./shape | c++filt  

I get the same output as earlier. Any other solution?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Stephen Jacob
  • 889
  • 1
  • 15
  • 33
  • [Name mangling](http://refspecs.linux-foundation.org/cxxabi-1.83.html#mangling) for types is not that complicated, and surely not in that case... I don't know what the answer to your question is, but a workaround is reading the type yourself. `P` pointer to `6Circle` Circle object (the 6 is the length of the name)... `P` pointer to `8triangle` triangle (8 characters). – David Rodríguez - dribeas Sep 25 '13 at 13:09
  • Hmm, that is simple. Thanks but just wanted to know if there is a cleaner way of getting the same – Stephen Jacob Sep 25 '13 at 13:26

2 Answers2

17

You need to use c++filt -t for types so the following should work:

./shape | c++filt -t

the man page for c++filt says the following for -t:

Attempt to demangle types as well as function names. This is disabled by default since mangled types are normally only used internally in the compiler, and they can be confused with non-mangled names. For example, a function called "a" treated as a mangled type name would be demangled to "signed char".

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

Which version of GCC (and its corresponding libstdc++) are you using?

With GCC 4.8, I have

static inline std::string 
 demangled_type_info_name(const std::type_info&ti)
{
  int status = 0;
  return abi::__cxa_demangle(ti.name(),0,0,&status);
}

and then I can use

std::cout << demangled_type_info_name(typeid(*ptr)) << std::endl;

where ptr points to some object with an RTTI (i.e. with some virtual methods, notably a virtual destructor).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547