Skip to content

Instantly share code, notes, and snippets.

@jdiego
Created August 25, 2018 00:39
Show Gist options
  • Save jdiego/affab070c4862ff880c55b2d73df8d90 to your computer and use it in GitHub Desktop.
Save jdiego/affab070c4862ff880c55b2d73df8d90 to your computer and use it in GitHub Desktop.
Printing C++ Types with __PRETTY__FUNCTION__. You can use this C++ trick to explore what the compiler is doing to your types, either for metaprogramming purposes or just to learn more about auto, auto&&, and decltype(auto)
#include <cstdio>
template<typename T>
void f(void)
{
puts(__PRETTY_FUNCTION__);
}
#define EXPLORE(expr) \
printf("decltype(" #expr ") is ..."); \
f<decltype(expr)>();
int main()
{
auto x = 5;
auto&& y = 5;
decltype(auto) z = 5;
EXPLORE(x); EXPLORE((x));
EXPLORE(y); EXPLORE((y));
EXPLORE(z); EXPLORE((z));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment