Skip to content

Instantly share code, notes, and snippets.

@Bktero
Last active December 15, 2023 09:50
Show Gist options
  • Save Bktero/7b595b2a84fec472d3cec9e5fe587905 to your computer and use it in GitHub Desktop.
Save Bktero/7b595b2a84fec472d3cec9e5fe587905 to your computer and use it in GitHub Desktop.
[C++] Demangling type name so that it is human-readable
#include <cxxabi.h>
#include <iostream>
#include <typeinfo>
// http://en.cppreference.com/w/cpp/types/type_info/name
// https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html
template<typename T>
void sayMyName() {
auto name = typeid(T).name();
std::cout << "Raw: " << name << '\n';
auto demanged = abi::__cxa_demangle(name, nullptr, nullptr, nullptr);
std::cout << "Demangled: " << demanged << '\n' << '\n';
std::free(demanged);
}
class Foo {
};
using Callback = std::pair<int, int>(*)(const Foo&);
int main() {
sayMyName<char>();
sayMyName<signed short>();
sayMyName<int>();
sayMyName<unsigned long long>();
sayMyName<float>();
sayMyName<double>();
sayMyName<Foo>();
sayMyName<Callback>();
}
Raw: c
Demangled: char
Raw: s
Demangled: short
Raw: i
Demangled: int
Raw: y
Demangled: unsigned long long
Raw: f
Demangled: float
Raw: d
Demangled: double
Raw: 3Foo
Demangled: Foo
Raw: PFSt4pairIiiERK3FooE
Demangled: std::pair<int, int> (*)(Foo const&)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment