Skip to content

Instantly share code, notes, and snippets.

@o11c
Created September 13, 2012 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o11c/3718491 to your computer and use it in GitHub Desktop.
Save o11c/3718491 to your computer and use it in GitHub Desktop.
Wrapper for c++ demangling
#include <cxxabi.h>
#include <typeinfo>
#include <string>
#include <stdexcept>
std::string demangle(const std::type_info& info)
{
int status;
char *result = abi::__cxa_demangle(info.name(), nullptr, 0, &status);
switch(status)
{
case 0:
break;
case -1:
throw std::bad_alloc();
case -2:
throw std::invalid_argument("bad name argument");
case -3:
throw std::invalid_argument("bad other argument?");
default:
abort();
}
try
{
std::string out = result;
free(result);
return std::move(out);
}
catch(...)
{
free(result);
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment