Home >Backend Development >C++ >Why Does `typeid.name()` Produce Different Output in GCC and How Can I Demangle It?
Typeid.name() Behavior in GCC and Demangling Techniques
When using the typeid operator, developers encounter differences in the output of typeid.name() depending on the compiler used. This article explores why this occurs in GCC and how to resolve the issue to print unmangled type names.
GCC vs. Visual C : Type Name Display
In the example provided, GCC (version 4.4.4) returns "4Blah" when calling typeid(Blah).name(), while Visual C 2008 displays "struct Blah." This discrepancy is due to GCC's return of a decorated type name.
GCC's Decorated Type Names
Implementations of the typeid operator are not uniformly defined. In the case of GCC, it returns a decorated type name, which includes additional information such as the type's size and alignment.
Unmangling Decorated Names
To retrieve the unmangled type name, users must de-mangling the decorated name. This can be accomplished using various tools:
Applying Demangling
In GCC, the following command can be executed to demangle the decorated name "4Blah":
g++ -fno-rtti -no-pie -o main main.cpp ./main | c++filt
This command will print "struct Blah" as the demangled type name. Integrating the __cxa_demangle() function into your code is also an option for programmatically demangling decorated names.
The above is the detailed content of Why Does `typeid.name()` Produce Different Output in GCC and How Can I Demangle It?. For more information, please follow other related articles on the PHP Chinese website!