more friendly static checks

This commit is contained in:
neargye 2020-02-14 15:28:16 +05:00
parent 9885d98bf3
commit 6e97229efd

View file

@ -378,7 +378,6 @@ constexpr auto n() noexcept {
return std::string_view{}; return std::string_view{};
} }
#else #else
static_assert(nameof_enum_supported<E>::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
return std::string_view{}; // Unsupported compiler. return std::string_view{}; // Unsupported compiler.
#endif #endif
} }
@ -527,7 +526,7 @@ constexpr auto strings() noexcept {
template <typename E> template <typename E>
class enum_traits { class enum_traits {
static_assert(is_enum_v<E>, "nameof::enum_traits requires enum type."); static_assert(is_enum_v<E>, "nameof::enum_traits requires enum type.");
static_assert(count_v<E> > 0, "nameof::enum_range requires enum implementation or valid max and min."); static_assert(count_v<E> > 0, "nameof::enum_range requires enum implementation and valid max and min.");
using U = std::underlying_type_t<E>; using U = std::underlying_type_t<E>;
inline static constexpr auto strings_ = strings<E>(); inline static constexpr auto strings_ = strings<E>();
inline static constexpr auto indexes_ = indexes<E>(std::make_integer_sequence<int, range_size_v<E>>{}); inline static constexpr auto indexes_ = indexes<E>(std::make_integer_sequence<int, range_size_v<E>>{});
@ -560,11 +559,9 @@ constexpr auto n() noexcept {
# elif defined(_MSC_VER) # elif defined(_MSC_VER)
constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)}; constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)};
# endif # endif
static_assert(name.size() > 0, "Type does not have a name.");
return cstring<name.size()>{name}; return cstring<name.size()>{name};
#else #else
static_assert(nameof_type_supported<T...>::value, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
return std::string_view{}; // Unsupported compiler. return std::string_view{}; // Unsupported compiler.
#endif #endif
} }
@ -583,6 +580,7 @@ inline constexpr bool is_nameof_enum_supported = detail::nameof_enum_supported<v
// Obtains simple (unqualified) string enum name of enum variable. // Obtains simple (unqualified) string enum name of enum variable.
template <typename E> template <typename E>
[[nodiscard]] constexpr auto nameof_enum(E value) noexcept -> detail::enable_if_enum_t<E, std::string_view> { [[nodiscard]] constexpr auto nameof_enum(E value) noexcept -> detail::enable_if_enum_t<E, std::string_view> {
static_assert(detail::nameof_enum_supported<E>::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
return detail::enums::enum_traits<detail::remove_cvref_t<E>>::name(value); return detail::enums::enum_traits<detail::remove_cvref_t<E>>::name(value);
} }
@ -590,7 +588,9 @@ template <typename E>
// This version is much lighter on the compile times and is not restricted to the enum_range limitation. // This version is much lighter on the compile times and is not restricted to the enum_range limitation.
template <auto V> template <auto V>
[[nodiscard]] constexpr auto nameof_enum() noexcept -> detail::enable_if_enum_t<decltype(V), std::string_view> { [[nodiscard]] constexpr auto nameof_enum() noexcept -> detail::enable_if_enum_t<decltype(V), std::string_view> {
constexpr std::string_view name = detail::enum_name_v<detail::remove_cvref_t<decltype(V)>, V>; using E = detail::remove_cvref_t<decltype(V)>;
static_assert(detail::nameof_enum_supported<E>::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
constexpr std::string_view name = detail::enum_name_v<E, V>;
static_assert(name.size() > 0, "Enum value does not have a name."); static_assert(name.size() > 0, "Enum value does not have a name.");
return name; return name;
@ -599,21 +599,31 @@ template <auto V>
// Obtains string name of type, reference and cv-qualifiers are ignored. // Obtains string name of type, reference and cv-qualifiers are ignored.
template <typename T> template <typename T>
[[nodiscard]] constexpr std::string_view nameof_type() noexcept { [[nodiscard]] constexpr std::string_view nameof_type() noexcept {
static_assert(detail::nameof_type_supported<T>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
#if defined(_MSC_VER) #if defined(_MSC_VER)
return detail::type_name_v<detail::identity<detail::remove_cvref_t<T>>>; using U = detail::identity<detail::remove_cvref_t<T>>;
#else #else
return detail::type_name_v<detail::remove_cvref_t<T>>; using U = detail::remove_cvref_t<T>;
#endif #endif
constexpr std::string_view name = detail::type_name_v<U>;
static_assert(name.size() > 0, "Type does not have a name.");
return name;
} }
// Obtains string name of full type, with reference and cv-qualifiers. // Obtains string name of full type, with reference and cv-qualifiers.
template <typename T> template <typename T>
[[nodiscard]] constexpr std::string_view nameof_full_type() noexcept { [[nodiscard]] constexpr std::string_view nameof_full_type() noexcept {
static_assert(detail::nameof_type_supported<T>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
#if defined(_MSC_VER) #if defined(_MSC_VER)
return detail::type_name_v<detail::identity<T>>; using U = detail::identity<T>;
#else #else
return detail::type_name_v<T>; using U = T;
#endif #endif
constexpr std::string_view name = detail::type_name_v<U>;
static_assert(name.size() > 0, "Type does not have a name.");
return name;
} }
} // namespace nameof } // namespace nameof