From 02e33b6b18f361a63e1bef0a5632c295c42ed24a Mon Sep 17 00:00:00 2001 From: neargye Date: Mon, 7 Sep 2020 20:35:21 +0300 Subject: [PATCH] add nameof_short_type --- example/example.cpp | 3 +++ include/nameof.hpp | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/example/example.cpp b/example/example.cpp index 850eebc..aaf7759 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -158,12 +158,15 @@ int main() { std::cout << NAMEOF_TYPE(const Long::LL&) << std::endl; // 'Long::LL' std::cout << nameof::nameof_full_type() << std::endl; // 'const Long::LL &' std::cout << NAMEOF_FULL_TYPE(const Long::LL&) << std::endl; // 'const Long::LL &' + std::cout << NAMEOF_SHORT_TYPE(const Long::LL&) << std::endl; // 'LL' + std::cout << NAMEOF_SHORT_TYPE(const SomeClass&) << std::endl; // 'SomeClass' // Nameof variable type. std::cout << nameof::nameof_type() << std::endl; // 'SomeStruct' std::cout << NAMEOF_TYPE_EXPR(structvar) << std::endl; // 'SomeStruct' std::cout << NAMEOF_TYPE_EXPR(std::declval>()) << std::endl; // 'SomeClass' std::cout << NAMEOF_FULL_TYPE_EXPR(std::declval>()) << std::endl; // 'const SomeClass &&' + std::cout << NAMEOF_SHORT_TYPE_EXPR(std::declval>()) << std::endl; // 'SomeClass' // Nameof macro. std::cout << NAMEOF(__LINE__) << std::endl; // '__LINE__' diff --git a/include/nameof.hpp b/include/nameof.hpp index 0a8c8a9..7158e3f 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -784,6 +784,17 @@ template return name; } +// Obtains string name of short type. +template +[[nodiscard]] constexpr std::string_view nameof_short_type() noexcept { + static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); + using U = detail::identity>; + constexpr std::string_view name = detail::pretty_name(detail::type_name_v); + static_assert(name.size() > 0, "Type does not have a name."); + + return name; +} + } // namespace nameof // Obtains simple (unqualified) string name of variable, function, macro. @@ -835,6 +846,12 @@ template // Obtains string name full type of expression, with reference and cv-qualifiers. #define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type() +// Obtains string name of short type. +#define NAMEOF_SHORT_TYPE(...) ::nameof::nameof_short_type<__VA_ARGS__>() + +// Obtains string name short type of expression. +#define NAMEOF_SHORT_TYPE_EXPR(...) ::nameof::nameof_short_type() + // Obtains string name of type, using RTTI. #define NAMEOF_TYPE_RTTI(...) ::nameof::detail::demangle(typeid(__VA_ARGS__).name())