Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I ungarble GCC's RTTI names?

Using gcc, when I ask for an object/variable's type using typeid, I get a different result from the type_info::name method from what I'd expect to get on Windows. I Googled around a bit, and found out that RTTI names are implementation-specific.

Problem is, I want to get a type's name as it would be returned on Windows. Is there an easy way to do this?

like image 551
Aleph Dvorak Avatar asked Jan 18 '10 10:01

Aleph Dvorak


People also ask

Does Typeid require RTTI?

Just like dynamic_cast, the typeid does not always need to use RTTI mechanism to work correctly. If the argument of the typeid expression is non-polymorphic type, then no runtime check is performed. Instead, the information about the type is known at the compile-time.

How do I enable RTTI?

RTTI will be enabled or disabled when compiling your program via compiler options - it's not something enabled or disabled in the Unix environment globally. The easiest way to see if it's enabled by default for your compiler is to just try compiling some code using RTTI.


2 Answers

If it's what you're asking, there is no compiler switch that would make gcc behave like msvc regarding the name returned by type_info::name().

However, in your code you can rely on the gcc specific __cxa_demangle function.

There is in fact an answer on SO that addresses your problem.

Reference: libstdc++ manual, Chapter 40. Demangling.

like image 56
Gregory Pakosz Avatar answered Sep 30 '22 02:09

Gregory Pakosz


c++ function names really include all the return and argument type information as well as the class and method name. When compiled, they are 'mangled' into a standard form (standard for each compiler) that can act as an assembler symbol and includes all the type information.

You need to run a function or program to reverse this mangling, called a demangler.

try running

c++filt myoutput.txt

on the output of the function. This demangles the real symbol name back into a human readable form.

like image 37
Alex Brown Avatar answered Sep 30 '22 04:09

Alex Brown