Chapter 28. Demangling

Transforming C++ ABI identifiers (like RTTI symbols) into the original C++ source identifiers is called demangling.

If you have read the source documentation for namespace abi then you are aware of the cross-vendor C++ ABI in use by GCC. One of the exposed functions is used for demangling, abi::__cxa_demangle.

In programs like c++filt, the linker, and other tools have the ability to decode C++ ABI names, and now so can you.

(The function itself might use different demanglers, but that's the whole point of abstract interfaces. If we change the implementation, you won't notice.)

Probably the only time you'll be interested in demangling at runtime is when you're seeing typeid strings in RTTI. For example:

#include <iostream>
#include <cstdlib>
#include <cxxabi.h>

struct empty { };

template <typename T, int N>
  struct bar { };

int main()
{
  int     status;
  char   *realname;

  // typeid
  bar<empty,17>          u;
  const std::type_info  &ti = typeid(u);

  realname = abi::__cxa_demangle(ti.name(), NULL, NULL, &status);
  std::cout << ti.name() << "\t=> " << realname << "\t: " << status << '\n';
  std::free(realname);
}
   

This prints

   
      3barI5emptyLi17EE       => bar<empty, 17>       : 0
   
   

The demangler interface is described in the source documentation linked to above. It is actually written in C, so you don't need to be writing C++ in order to demangle C++. (That also means we have to use crummy memory management facilities, so don't forget to free() the returned char array.)