Home > Backend Development > C++ > body text

Why Does `typeid.name()` Produce Different Output in GCC and How Can I Demangle It?

Susan Sarandon
Release: 2024-11-21 07:20:10
Original
307 people have browsed it

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:

  • c filt command: A command-line utility that demangles decorated names.
  • __cxa_demangle() function: A C function that performs demangling.

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template