Skip to content

Instantly share code, notes, and snippets.

@michael-martinez
Created December 14, 2020 10:01
Show Gist options
  • Save michael-martinez/e3f47db71915219807926dd0e39bdae5 to your computer and use it in GitHub Desktop.
Save michael-martinez/e3f47db71915219807926dd0e39bdae5 to your computer and use it in GitHub Desktop.
C++ Type Inspector for C++17
#include <string_view>
template <typename T>
constexpr auto type_name() noexcept {
std::string_view name = "Error: unsupported compiler", prefix, suffix;
#ifdef __clang__
name = __PRETTY_FUNCTION__;
prefix = "auto type_name() [T = ";
suffix = "]";
#elif defined(__GNUC__)
name = __PRETTY_FUNCTION__;
prefix = "constexpr auto type_name() [with T = ";
suffix = "]";
#elif defined(_MSC_VER)
name = __FUNCSIG__;
prefix = "auto __cdecl type_name<";
suffix = ">(void) noexcept";
#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