add nameof_short_type

This commit is contained in:
neargye 2020-09-07 20:35:21 +03:00
parent e814a3c13d
commit 02e33b6b18
2 changed files with 20 additions and 0 deletions

View file

@ -158,12 +158,15 @@ int main() {
std::cout << NAMEOF_TYPE(const Long::LL&) << std::endl; // 'Long::LL' std::cout << NAMEOF_TYPE(const Long::LL&) << std::endl; // 'Long::LL'
std::cout << nameof::nameof_full_type<const Long::LL&>() << std::endl; // 'const Long::LL &' std::cout << nameof::nameof_full_type<const Long::LL&>() << std::endl; // 'const Long::LL &'
std::cout << NAMEOF_FULL_TYPE(const Long::LL&) << 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<int>&) << std::endl; // 'SomeClass'
// Nameof variable type. // Nameof variable type.
std::cout << nameof::nameof_type<decltype(structvar)>() << std::endl; // 'SomeStruct' std::cout << nameof::nameof_type<decltype(structvar)>() << std::endl; // 'SomeStruct'
std::cout << NAMEOF_TYPE_EXPR(structvar) << std::endl; // 'SomeStruct' std::cout << NAMEOF_TYPE_EXPR(structvar) << std::endl; // 'SomeStruct'
std::cout << NAMEOF_TYPE_EXPR(std::declval<const SomeClass<int>>()) << std::endl; // 'SomeClass<int>' std::cout << NAMEOF_TYPE_EXPR(std::declval<const SomeClass<int>>()) << std::endl; // 'SomeClass<int>'
std::cout << NAMEOF_FULL_TYPE_EXPR(std::declval<const SomeClass<int>>()) << std::endl; // 'const SomeClass<int> &&' std::cout << NAMEOF_FULL_TYPE_EXPR(std::declval<const SomeClass<int>>()) << std::endl; // 'const SomeClass<int> &&'
std::cout << NAMEOF_SHORT_TYPE_EXPR(std::declval<const SomeClass<int>>()) << std::endl; // 'SomeClass'
// Nameof macro. // Nameof macro.
std::cout << NAMEOF(__LINE__) << std::endl; // '__LINE__' std::cout << NAMEOF(__LINE__) << std::endl; // '__LINE__'

View file

@ -784,6 +784,17 @@ template <typename T>
return name; return name;
} }
// Obtains string name of short type.
template <typename T>
[[nodiscard]] constexpr std::string_view nameof_short_type() noexcept {
static_assert(detail::nameof_type_supported<T>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
using U = detail::identity<detail::remove_cvref_t<T>>;
constexpr std::string_view name = detail::pretty_name(detail::type_name_v<U>);
static_assert(name.size() > 0, "Type does not have a name.");
return name;
}
} // namespace nameof } // namespace nameof
// Obtains simple (unqualified) string name of variable, function, macro. // Obtains simple (unqualified) string name of variable, function, macro.
@ -835,6 +846,12 @@ template <typename T>
// Obtains string name full type of expression, with reference and cv-qualifiers. // Obtains string name full type of expression, with reference and cv-qualifiers.
#define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type<decltype(__VA_ARGS__)>() #define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type<decltype(__VA_ARGS__)>()
// 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<decltype(__VA_ARGS__)>()
// Obtains string name of type, using RTTI. // Obtains string name of type, using RTTI.
#define NAMEOF_TYPE_RTTI(...) ::nameof::detail::demangle(typeid(__VA_ARGS__).name()) #define NAMEOF_TYPE_RTTI(...) ::nameof::detail::demangle(typeid(__VA_ARGS__).name())