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?
Transforming C++ ABI identifiers (like RTTI symbols) into the original C++ source identifiers is called “demangling.”
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.
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".
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With