Last active
August 2, 2021 12:06
-
-
Save aianau/2c18389b38504be012162ebf712ebee4 to your computer and use it in GitHub Desktop.
get type of variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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