Skip to content

Instantly share code, notes, and snippets.

@PaulChana
Last active February 15, 2018 09:26
Show Gist options
  • Save PaulChana/15a70221728f0e10c456667d9712454a to your computer and use it in GitHub Desktop.
Save PaulChana/15a70221728f0e10c456667d9712454a to your computer and use it in GitHub Desktop.
[Demangle C++ types] Demangle #C++
#include <memory>
#include <string>
#include <cxxabi.h>
class Demangler
{
public:
template <class T>
static std::string compute_object_name (T& obj)
{
const std::string mangled_name = typeid (obj).name ();
int status = -1;
std::unique_ptr<char, void(*)(void*)> res
{
abi::__cxa_demangle (mangled_name.c_str(),
nullptr,
nullptr,
&status),
std::free
};
if (status == 0)
return res.get();
return mangled_name;
}
};
/*
Example usage:
class Foo
{
public:
Foo() = default;
~Foo() = default;
void print_name() const { std::cout << Demangler::compute_object_name (*this) << std::endl; }
};
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment