Advertisement
pneave

Display variable type (C++14)

Apr 12th, 2021
1,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. #include <string_view>
  3. //-----------------------------------------------------------------------------
  4. //-----------------------------------------------------------------------------
  5.  
  6. // Usage:
  7. //    auto some_type = ...;
  8. //    type_name<decltype(some_type)>();
  9. //
  10.  
  11. template <typename T>
  12. constexpr auto type_name() noexcept
  13. {
  14.    std::string_view name = "Error: unsupported compiler", prefix, suffix;
  15. #ifdef __clang__
  16.    name = __PRETTY_FUNCTION__;
  17.    prefix = "auto type_name() [T = ";
  18.    suffix = "]";
  19. #elif defined(__GNUC__)
  20.    name = __PRETTY_FUNCTION__;
  21.    prefix = "constexpr auto type_name() [with T = ";
  22.    suffix = "]";
  23. #elif defined(_MSC_VER)
  24.    name = __FUNCSIG__;
  25.    prefix = "auto __cdecl type_name<";
  26.    suffix = ">(void) noexcept";
  27. #endif
  28.    name.remove_prefix(prefix.size());
  29.    name.remove_suffix(suffix.size());
  30.    return name;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement