pneave icon

Display variable type (C++14)

pneave | PRO | 04/12/21 11:13:08 AM UTC | 0 ⭐ | 1287 👁️ | Never ⏰ | []
C++ |

940 B

|

None

|

0 👍

/

0 👎

//-----------------------------------------------------------------------------
#include <string_view>
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
 
// Usage:
//    auto some_type = ...;
//    type_name<decltype(some_type)>();
//
 
template <typename T>
constexpr auto type_name() noexcept 
{
   std::string_view name = "Error: unsupported compiler", prefix, suffix;
#ifdef __clang__
   name = __PRETTY_FUNCTION__;
   prefix = "auto type_name() [T = ";
   suffix = "]";
#elif defined(__GNUC__)
   name = __PRETTY_FUNCTION__;
   prefix = "constexpr auto type_name() [with T = ";
   suffix = "]";
#elif defined(_MSC_VER)
   name = __FUNCSIG__;
   prefix = "auto __cdecl type_name<";
   suffix = ">(void) noexcept";
#endif
   name.remove_prefix(prefix.size());
   name.remove_suffix(suffix.size());
   return name;
}
 

Comments