Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++filt does not demangle typeid name

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?

like image 880
Stephen Jacob Avatar asked Sep 25 '13 13:09

Stephen Jacob


People also ask

What does demangle mean?

Transforming C++ ABI identifiers (like RTTI symbols) into the original C++ source identifiers is called “demangling.”

What is C ++ Filt?

The c++filt utility is a filter that copies characters from file names or standard input to standard output, replacing all mangled names with their corresponding demangled names.


2 Answers

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".

like image 98
Shafik Yaghmour Avatar answered Oct 12 '22 01:10

Shafik Yaghmour


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

like image 43
Basile Starynkevitch Avatar answered Oct 12 '22 00:10

Basile Starynkevitch