Skip to content

Instantly share code, notes, and snippets.

@halfelf
Created May 14, 2019 05:02
Show Gist options
  • Save halfelf/801df7fa7d0f1fb6a1c16a7b405b0021 to your computer and use it in GitHub Desktop.
Save halfelf/801df7fa7d0f1fb6a1c16a7b405b0021 to your computer and use it in GitHub Desktop.
Get C++ type name
template <class T>
constexpr std::string_view type_name() {
#ifdef __clang__
std::string_view p = __PRETTY_FUNCTION__;
return std::string_view(p.data() + 34, p.size() - 34 - 1);
#elif defined(__GNUC__)
std::string_view p = __PRETTY_FUNCTION__;
# if __cplusplus < 201402
return std::string_view(p.data() + 36, p.size() - 36 - 1);
# else
return std::string_view(p.data() + 49, p.find(';', 49) - 49);
# endif
#elif defined(_MSC_VER)
std::string_view p = __FUNCSIG__;
return std::string_view(p.data() + 84, p.size() - 84 - 7);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment