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

7

61

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?

Benedictus answered 16/12, 2010 at 22:13 Comment(2)
possible duplicate of Print variable type in C++Gae
Possible duplicate of Unmangling the result of std::type_info::nameBritannic
H
75

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.

Histone answered 16/12, 2010 at 22:18 Comment(2)
specifically c++filt -tSublet
Ugh. Goddamn it GCC.Zoroastrian
C
19

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
Casey answered 16/12, 2010 at 22:20 Comment(2)
And here's a example: assume the content of stdout is PFPFiiEiiE, we can use the shell command echo "PFPFiiEiiE" | c++filt, and the result would be int (* (*)(int, int))(int).Huey
You might (e.g on Ubuntu) also need to add --types to demangle type endcodings: echo "PFPFiiEiiE" | c++filt --typesEdva
S
13

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.

Scrupulous answered 16/12, 2010 at 22:17 Comment(2)
in my program, I just want to log my struct name to a logfile. Following the above example, I could have typed something like: LOG("Blah");. But I want to account for possibly changing the struct name in the future, so I'm trying to do: LOG(typeinfo(Blah).name();Benedictus
@ShaChris23: Well, I guess then you'll just have to live with not all compilers doing it in the same way. Have a look, however, what the runtime lib emits if, instead of blah, you have a map of strings to vectors of lists of strings. That can very easily exceed any sensible limits for log file size.Scrupulous
F
8

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.

Furness answered 16/12, 2010 at 22:24 Comment(0)
P
6

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.

Persist answered 2/6, 2016 at 12:38 Comment(1)
you are right bro.Noma
S
3

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.

Stringfellow answered 16/12, 2010 at 22:17 Comment(2)
No. It may not return empty string for every type. The way the string is represented is application defined but according to C++ standard it must differ a type from others. With empty strings you can't do it.Devault
@RodrigoGurgel No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.Brunn

© 2022 - 2024 — McMap. All rights reserved.