简体   繁体   English

c ++ filt不会解码typeid名称

[英]c++filt does not demangle typeid name

I am running a code on GCC C++ compiler, to output the type_info::name: 我在GCC C ++编译器上运行代码,输出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? 还有其他方法吗?

You need to use c++filt -t for types so the following should work: 您需要对类型使用c++filt -t ,因此以下内容应该有效:

./shape | c++filt -t

the man page for c++filt says the following for -t : c ++ filt手册页-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". 例如,一个被称为“a”的函数被视为一个受损的类型名称,将被解构为“signed char”。

Which version of GCC (and its corresponding libstdc++ ) are you using? 您使用的是哪个版本的GCC(及其相应的libstdc ++ )?

With GCC 4.8, I have 有了GCC 4.8,我有

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 (ie with some virtual methods, notably a virtual destructor). 其中ptr使用RTTI指向某个对象(即使用某些虚拟方法,尤其是虚拟析构函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM