c++gccg++rtti

Why does typeid.name() return weird characters using GCC and how to make it print unmangled names?


How come when I run this main.cpp:

#include <iostream>
#include <typeinfo>

using namespace std;

struct Blah {};

int main() {
  cout << typeid(Blah).name() << endl;
  return 0;
}

By compiling it with GCC version 4.4.4:

g++ main.cpp

I get this:

4Blah

On Visual C++ 2008, I would get:

struct Blah

Is there a way to make it just print Blah or struct Blah?


Solution

  • The return of name is implementation defined : an implementation is not even required to return different strings for different types.

    What you get from g++ is a decorated name, that you can "demangle" using the c++filt command or __cxa_demangle.