Skip to content

Instantly share code, notes, and snippets.

@aianau
Last active August 2, 2021 12:06
Show Gist options
  • Save aianau/2c18389b38504be012162ebf712ebee4 to your computer and use it in GitHub Desktop.
Save aianau/2c18389b38504be012162ebf712ebee4 to your computer and use it in GitHub Desktop.
get type of variable
#include <iostream>
#include <string_view>
template <class T>
constexpr std::string_view type_name()
{
using namespace std;
#ifdef __clang__
string_view p = __PRETTY_FUNCTION__;
return string_view(p.data() + 34, p.size() - 34 - 1);
#elif defined(__GNUC__)
string_view p = __PRETTY_FUNCTION__;
# if __cplusplus < 201402
return string_view(p.data() + 36, p.size() - 36 - 1);
# else
return string_view(p.data() + 49, p.find(';', 49) - 49);
# endif
#elif defined(_MSC_VER)
string_view p = __FUNCSIG__;
return string_view(p.data() + 84, p.size() - 84 - 7);
#endif
}
enum class Foo
{
foo1,
foo2
};
int main()
{
auto a = Foo::foo1;
auto&& b = static_cast<int&&>(3);
std::cout << type_name<decltype(a)>() << "\n";
std::cout << type_name<decltype(b)>() << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment