Skip to content

Instantly share code, notes, and snippets.

@jfalcou
Created December 20, 2019 19:58
Show Gist options
  • Save jfalcou/2da943d75737294620cf35764a0950fa to your computer and use it in GitHub Desktop.
Save jfalcou/2da943d75737294620cf35764a0950fa to your computer and use it in GitHub Desktop.
#include <string_view>
#include <iostream>
template<typename T> struct name_of_
{
static constexpr auto value() noexcept
{
#if defined(_MSC_VER )
std::string_view data(__FUNCSIG__);
auto i = data.find('<') + 1,
j = data.find(">::value");
return data.substr(i, j - i);
#else
std::string_view data(__PRETTY_FUNCTION__);
auto i = data.find('=') + 2,
j = data.find_last_of(']');
return data.substr(i, j - i);
#endif
}
};
template<typename T>
inline constexpr auto const name_of = name_of_<T>::value();
int main()
{
auto x = name_of<std::string>;
std::cout << x << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment