Skip to content

Instantly share code, notes, and snippets.

@willwray
Created September 20, 2018 14:13
Show Gist options
  • Save willwray/902f12c4d88a0ea7cb52c19b0f1749da to your computer and use it in GitHub Desktop.
Save willwray/902f12c4d88a0ea7cb52c19b0f1749da to your computer and use it in GitHub Desktop.
MSVC test pretty print of type and auto template args
#include <iostream>
#include <string_view>
// typesig_s<T>() size of full pretty print output with template type arg T
template <typename>
constexpr auto typesig_s()
{
return sizeof(__FUNCSIG__);
}
// type_name<T>() type name of template type arg T, from pretty-print output
template <typename>
constexpr auto type_name()
{
std::string_view ret(__FUNCSIG__);
constexpr auto prefix_size = typesig_s<int>()
- sizeof( "int>(void)");
constexpr auto suffix_size = sizeof( ">(void)")-1;
ret.remove_prefix(prefix_size);
ret.remove_suffix(suffix_size);
return ret;
}
// autosig_s<v>() size of full pretty print output with template non-type arg v
template <auto>
constexpr auto autosig_s()
{
return sizeof(__FUNCSIG__);
}
// auto_name<args...>() name of template non-type args..., from pretty-print output
template <auto...>
constexpr auto auto_name()
{
std::string_view ret(__FUNCSIG__);
constexpr auto prefix_size = autosig_s<nullptr>()
- sizeof ("nullptr>(void)");
constexpr auto suffix_size = sizeof( ">(void)") - 1;
ret.remove_prefix(prefix_size);
ret.remove_suffix(suffix_size);
return ret;
}
#define TYPE_NAME(Type) std::cout << "type_name <" << #Type << ">\n"\
" -> " << type_name< Type >()
#define AUTO_NAME(...) std::cout << "auto_name <" << #__VA_ARGS__ << ">\n"\
" -> " << auto_name< __VA_ARGS__ >()
struct C { bool b; char c(); }; // To test pointer-to-member
enum e { a, b, c }; // Unscoped global
enum class E { a, b, c }; // Scoped global
struct U { enum e { a, b, c }; }; // Unscoped member
struct S { enum class E { a, b, c }; }; // Scoped member
namespace N { enum { a, b, c }; } // Namespace anon
int main()
{
enum m { a, b, c }; // Unscoped local
enum class M { a, b, c }; // Scoped local
std::cout << "Issue 339663 tests: enumerator pretty-print\n"
"------------------\n";
AUTO_NAME(::a,::b,::c,e(3)) << '\n';
AUTO_NAME(E::a,E::b,E::c,E{4}) << '\n';
AUTO_NAME(U::a,U::b,U::c,U::e(5)) << '\n';
AUTO_NAME(S::E::a,S::E::b,S::E::c,S::E(6)) << '\n';
AUTO_NAME(N::a,N::b,N::c,decltype(N::a)(7)) << '\n';
AUTO_NAME(a,b,c,m(9)) << '\n';
AUTO_NAME(M::a,M::b,M::c,M(10)) << '\n';
std::cout << "\nIssue 339714 test: nullptr pretty-print\n"
"-----------------\n";
AUTO_NAME(nullptr) << '\n';
TYPE_NAME(decltype(nullptr)) << " ....... -> ********** Wrong! Type is printed as value! ********\n";
std::cout << "\nPointer-to-member pretty printing:\n"
"-----------------\n";
AUTO_NAME(&C::b,&C::c) << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment