Skip to content

Instantly share code, notes, and snippets.

@mnagy
Created September 14, 2012 13:41
Show Gist options
  • Save mnagy/3721977 to your computer and use it in GitHub Desktop.
Save mnagy/3721977 to your computer and use it in GitHub Desktop.
Utility function for demangling C++ type names with RTTI
#include <cxxabi.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
std::string demangleTypeName(const char *name) {
int status;
char *realname;
std::string ret;
realname = abi::__cxa_demangle(name, 0, 0, &status);
if (status != 0)
return std::string(name);
ret = realname;
free(realname);
return ret;
}
#define type_name(expr) demangleTypeName(typeid(expr).name())
#include <iostream>
int main(void)
{
std::string::const_iterator foo;
std::cout << "Mangled: " << typeid(foo).name() << std::endl;
std::cout << "Demangled: " << type_name(foo) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment