Skip to content

Instantly share code, notes, and snippets.

@maxtruxa
Created December 16, 2022 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxtruxa/3f1f544650b76f3c2ca07f9bcc6600a7 to your computer and use it in GitHub Desktop.
Save maxtruxa/3f1f544650b76f3c2ca07f9bcc6600a7 to your computer and use it in GitHub Desktop.
Get the pretty typename of any C++ type at compile time. Demo: https://godbolt.org/z/vsaKsecW6
#include <string_view>
template <typename T>
#if __cpp_consteval
consteval
#else
constexpr
#endif
auto type_name() {
#ifdef __clang__
std::string_view name = __PRETTY_FUNCTION__;
std::string_view prefix = "auto type_name() [T = ";
std::string_view suffix = "]";
#elif defined __GNUC__
std::string_view name = __PRETTY_FUNCTION__;
std::string_view prefix = "constexpr auto type_name() [with T = ";
std::string_view suffix = "]";
#elif defined _MSC_VER
std::string_view name = __FUNCSIG__;
std::string_view prefix = "auto __cdecl type_name<";
std::string_view suffix = ">(void)";
#else
# error Unsupported compiler.
#endif
name.remove_prefix(prefix.size());
name.remove_suffix(suffix.size());
return name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment