Skip to content

Instantly share code, notes, and snippets.

@socantre
Last active September 26, 2016 16:39
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 socantre/e7e54dcf3c5209f14dbb786c93cd71b6 to your computer and use it in GitHub Desktop.
Save socantre/e7e54dcf3c5209f14dbb786c93cd71b6 to your computer and use it in GitHub Desktop.
Example use of the Itanium ABI demangling API.
#include <cxxabi.h>
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <memory>
struct clib_free {
template<typename T>
void operator() (T *t) {
std::free(t);
}
};
static std::string demangle(char const *mangled) {
int status;
std::size_t n;
std::unique_ptr<char[], clib_free> demangled(abi::__cxa_demangle(mangled, nullptr, &n, &status));
switch (status) {
case -1:
throw std::bad_alloc();
case -2:
throw std::runtime_error("invalid mangled name");
case -3:
abort();
}
return std::string(demangled.get(), n-1);
}
int main() {
std::cout << demangle("_Z3foodc") << '\n';
std::cout << demangle("i") << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment