Created
September 14, 2012 13:41
-
-
Save mnagy/3721977 to your computer and use it in GitHub Desktop.
Utility function for demangling C++ type names with RTTI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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