Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2013 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4459038 to your computer and use it in GitHub Desktop.
Save anonymous/4459038 to your computer and use it in GitHub Desktop.
#include <type_info>
#include <memory>
#include <string>
#include <stdexcept>
class bad_mangled_name : public std::runtime_error {
public:
bad_mangled_name() : std::runtime_error{"bad name"} {}
};
std::string demangle(std::string const& name) {
int status;
std::unique_ptr<char, decltype(&std::free)> realname{abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status), &std::free};
switch (status) {
case 0: return realname.get();
case -1: throw std::bad_alloc{};
case -2: throw demangling_error{};
}
}
template<class T>
std::string name_of_type() {
return demangle(typeid(T).name());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment