Skip to content

Instantly share code, notes, and snippets.

@Bktero
Last active January 27, 2023 08:47
Show Gist options
  • Save Bktero/33c636dc60ab00f7a31696b735f34caa to your computer and use it in GitHub Desktop.
Save Bktero/33c636dc60ab00f7a31696b735f34caa to your computer and use it in GitHub Desktop.
[C++] Helper function to get the demangled name of a type as a string (for gcc)
#pragma once
#include <string>
#include <cxxabi.h>
template<typename T>
std::string demangleTemplateType() {
const auto typeName = typeid(T).name();
const auto demangledName = abi::__cxa_demangle(typeName, nullptr, nullptr, nullptr);
const auto name = demangledName ? demangledName : typeName;
std::string string{name};
free(demangledName); // required by the __cxa_demangle()'s documentation
return string;
}
#include <iostream>
using std::cout;
int main() {
std::cout << demangleTemplateType<int>() << '\n';
std::cout << demangleTemplateType<float>() << '\n';
std::cout << demangleTemplateType<std::string>() << '\n';
std::cout << demangleTemplateType<unsigned char>() << '\n';
}
int
float
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
unsigned char
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment