From 2b92a0db8c3f171b6f947b749fa3a3c14e309d62 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 8 Jun 2023 14:47:01 +0200 Subject: concepts: replace is_*_*equality_comparable --- source/lib/include/newtype/concepts.hpp | 31 ++++++++++++ .../newtype/impl/type_traits_extensions.hpp | 59 ---------------------- source/lib/include/newtype/newtype.hpp | 34 ++++++------- source/tests/src/equality_comparison.cpp | 15 +++--- 4 files changed, 55 insertions(+), 84 deletions(-) diff --git a/source/lib/include/newtype/concepts.hpp b/source/lib/include/newtype/concepts.hpp index a29ba37..6db528c 100644 --- a/source/lib/include/newtype/concepts.hpp +++ b/source/lib/include/newtype/concepts.hpp @@ -7,6 +7,37 @@ namespace nt::concepts { + + template + concept equality_comparable = requires(SubjectType lhs, SubjectType rhs) { + { + lhs == rhs + } -> std::convertible_to; + }; + + template + concept nothrow_equality_comparable = requires(SubjectType lhs, SubjectType rhs) { + requires equality_comparable; + { + lhs == rhs + } noexcept; + }; + + template + concept inequality_comparable = requires(SubjectType lhs, SubjectType rhs) { + { + lhs != rhs + } -> std::convertible_to; + }; + + template + concept nothrow_inequality_comparable = requires(SubjectType lhs, SubjectType rhs) { + requires inequality_comparable; + { + lhs != rhs + } noexcept; + }; + template concept hashable = requires(SubjectType subject) { { diff --git a/source/lib/include/newtype/impl/type_traits_extensions.hpp b/source/lib/include/newtype/impl/type_traits_extensions.hpp index bdd1cba..c4b9bee 100644 --- a/source/lib/include/newtype/impl/type_traits_extensions.hpp +++ b/source/lib/include/newtype/impl/type_traits_extensions.hpp @@ -11,65 +11,6 @@ namespace nt::impl { - inline namespace equality_comparable - { - - template - struct is_equality_comparable : std::false_type - { - }; - - template - struct is_equality_comparable() == std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_equality_comparable_v = is_equality_comparable::value; - - template - struct is_nothrow_equality_comparable : std::false_type - { - }; - - template - struct is_nothrow_equality_comparable() == std::declval())>> - : std::bool_constant() == std::declval())> - { - }; - - template - auto constexpr is_nothrow_equality_comparable_v = is_nothrow_equality_comparable::value; - - template - struct is_inequality_comparable : std::false_type - { - }; - - template - struct is_inequality_comparable() != std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_inequality_comparable_v = is_inequality_comparable::value; - - template - struct is_nothrow_inequality_comparable : std::false_type - { - }; - - template - struct is_nothrow_inequality_comparable() != std::declval())>> - : std::bool_constant() != std::declval())> - { - }; - - template - auto constexpr is_nothrow_inequality_comparable_v = is_nothrow_inequality_comparable::value; - - } // namespace equality_comparable - inline namespace relationally_comparable { diff --git a/source/lib/include/newtype/newtype.hpp b/source/lib/include/newtype/newtype.hpp index b833c10..126c439 100644 --- a/source/lib/include/newtype/newtype.hpp +++ b/source/lib/include/newtype/newtype.hpp @@ -251,54 +251,52 @@ namespace nt } }; - template + template auto constexpr operator==(new_type const & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_equality_comparable_v) - -> std::enable_if_t, bool> + new_type const & rhs) noexcept(nt::concepts::nothrow_equality_comparable) -> bool { return lhs.decay() == rhs.decay(); } - template + template + requires(DerivationClause(nt::EqBase)) auto constexpr operator==(new_type const & lhs, - BaseType const & rhs) noexcept(impl::is_nothrow_equality_comparable_v) - -> std::enable_if_t, bool> + BaseType const & rhs) noexcept(nt::concepts::nothrow_equality_comparable) -> bool { return lhs.decay() == rhs; } - template + template + requires(DerivationClause(nt::EqBase)) auto constexpr operator==(BaseType const & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_equality_comparable_v) - -> std::enable_if_t, bool> + new_type const & rhs) noexcept(nt::concepts::nothrow_equality_comparable) -> bool { return lhs == rhs.decay(); } - template + template auto constexpr operator!=(new_type const & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_inequality_comparable_v) - -> std::enable_if_t, bool> + new_type const & rhs) noexcept(nt::concepts::nothrow_inequality_comparable) -> bool { return lhs.decay() != rhs.decay(); } - template + template + requires(DerivationClause(nt::EqBase)) auto constexpr operator!=(new_type const & lhs, - BaseType const & rhs) noexcept(impl::is_nothrow_inequality_comparable_v) - -> std::enable_if_t, bool> + BaseType const & rhs) noexcept(nt::concepts::nothrow_inequality_comparable) -> bool { return lhs.decay() != rhs; } - template + template + requires(DerivationClause(nt::EqBase)) auto constexpr operator!=(BaseType const & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_inequality_comparable_v) - -> std::enable_if_t, bool> + new_type const & rhs) noexcept(nt::concepts::nothrow_inequality_comparable) -> bool { return lhs != rhs.decay(); } diff --git a/source/tests/src/equality_comparison.cpp b/source/tests/src/equality_comparison.cpp index d857d9b..0876dc2 100644 --- a/source/tests/src/equality_comparison.cpp +++ b/source/tests/src/equality_comparison.cpp @@ -1,3 +1,4 @@ +#include "newtype/concepts.hpp" #include "newtype/derivable.hpp" #include "newtype/deriving.hpp" #include "newtype/newtype.hpp" @@ -64,17 +65,17 @@ SCENARIO("Equality Comparison", "[compare]") { using type_alias = nt::new_type; - static_assert(noexcept(std::declval() == std::declval())); - static_assert(noexcept(std::declval() != std::declval())); + static_assert(nt::concepts::nothrow_equality_comparable); + static_assert(nt::concepts::nothrow_inequality_comparable); THEN("it is nothrow-equality-comparable") { - STATIC_REQUIRE(noexcept(std::declval() == std::declval())); + STATIC_REQUIRE(nt::concepts::nothrow_equality_comparable); } THEN("it is nothrow-inequality-comparable") { - STATIC_REQUIRE(noexcept(std::declval() != std::declval())); + STATIC_REQUIRE(nt::concepts::nothrow_inequality_comparable); } } @@ -88,12 +89,12 @@ SCENARIO("Equality Comparison", "[compare]") using type_alias = nt::new_type; - static_assert(!noexcept(std::declval() == std::declval())); - static_assert(!noexcept(std::declval() != std::declval())); + static_assert(!nt::concepts::nothrow_equality_comparable); + static_assert(!nt::concepts::nothrow_inequality_comparable); THEN("it is not nothrow-equality-comparable") { - STATIC_REQUIRE_FALSE(noexcept(std::declval() == std::declval())); + STATIC_REQUIRE_FALSE(nt::concepts::nothrow_equality_comparable); } THEN("it is not nothrow-inequality-comparable") -- cgit v1.2.3