c++filt does not demangle typeid name

馋奶兔 提交于 2019-12-10 13:23:09

问题


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?


回答1:


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




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



来源:https://stackoverflow.com/questions/19005744/cfilt-does-not-demangle-typeid-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!