Skip to content

Instantly share code, notes, and snippets.

@jeremy-rifkin
Created September 8, 2021 03:05
Show Gist options
  • Save jeremy-rifkin/592ca33c4bf4aac078eae02160a2712e to your computer and use it in GitHub Desktop.
Save jeremy-rifkin/592ca33c4bf4aac078eae02160a2712e to your computer and use it in GitHub Desktop.
Short utility to get the name of a type in C++
template<class T> constexpr std::string_view type_name() {
// clang: std::string_view ns::type_name() [T = int]
// gcc: constexpr std::string_view ns::type_name() [with T = int; std::string_view = std::basic_string_view<char>]
// msvc: const char *__cdecl ns::type_name<int>(void)
auto substring_bounded_by = [](std::string_view sig, std::string_view l, std::string_view r) {
assert(sig.find(l) != std::string_view::npos);
assert(sig.rfind(r) != std::string_view::npos);
assert(sig.find(l) < sig.rfind(r));
auto i = sig.find(l) + l.length();
return sig.substr(i, sig.rfind(r) - i);
};
#if defined(__clang__)
return substring_bounded_by(__PRETTY_FUNCTION__, "[T = ", "]");
#elif defined(__GNUC__) || defined(__GNUG__)
return substring_bounded_by(__PRETTY_FUNCTION__, "[with T = ", "; std::string_view = ");
#elif defined(_MSC_VER)
return substring_bounded_by(__FUNCSIG__, "type_name<", ">(void)");
#else
// TODO: ?
return __PRETTY_FUNCTION__;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment