Created
December 16, 2022 18:51
-
-
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
This file contains 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 <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