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

C++GccG++Rtti

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

The string returned is implementation defined.

What gcc is doing is returning the mangled name.
You can convert the mangled name into plain text with c++filt

> a.out | c++filt

Solution 3 - C++

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

No. The result of std::typeinfo::name() is unspecified. It might even return the same string for all types (or, indeed, empty strings for all types) and the implementation would still be standard-conforming. You must not rely on its result. Really, the only thing I found it useful for was debugging.

Tell us what what you need it for. Often traits is what you use instead.

Solution 4 - C++

As others have said, the result here is implementation-defined, meaning that the implementation (i.e., the compiler toolchain) is free to define it how it wants, so long as it documents that somewhere.

From the C++ standard, section 18.5.1/1 [lib.type.info]:

> The class type_info describes type information generated by the implementation. Objects of this class effectively store a pointer to a name for the type, and an encoded value suitable for comparing two types for equality or collating order. The names, encoding rule, and collating sequence for types are all unspecified and may differ between programs.

Solution 5 - C++

in 4Blah, 4 is the number of letters in your class name. For example if your class name is myEmptyClass then it would print 12myEmptyClass.

Solution 6 - C++

typeid().name() is implementation dependent. It may even return empty string for every type. That would not be very useful implementation, but it would be valid.

Solution 7 - C++

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsivabudhView Question on Stackoverflow
Solution 1 - C++icecrimeView Answer on Stackoverflow
Solution 2 - C++Martin YorkView Answer on Stackoverflow
Solution 3 - C++sbiView Answer on Stackoverflow
Solution 4 - C++Adam RosenfieldView Answer on Stackoverflow
Solution 5 - C++Pradeep RohillaView Answer on Stackoverflow
Solution 6 - C++Juraj BlahoView Answer on Stackoverflow
Solution 7 - C++Abhinav GhoshView Answer on Stackoverflow