Last active
January 27, 2023 08:47
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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