pneave

Display variable type (C++14)

Apr 12th, 2021
893
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×