From bc83f6dd1330b6b9cb39c8600242d17ff964de36 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 8 Jun 2023 18:09:00 +0200 Subject: concepts: replace arithmetic traits --- source/lib/include/newtype/concepts.hpp | 65 ++++++++++++ .../newtype/impl/type_traits_extensions.hpp | 113 --------------------- source/lib/include/newtype/newtype.hpp | 24 ++--- source/tests/src/arithmetic.cpp | 32 +++--- 4 files changed, 93 insertions(+), 141 deletions(-) diff --git a/source/lib/include/newtype/concepts.hpp b/source/lib/include/newtype/concepts.hpp index a50b2b3..42713f5 100644 --- a/source/lib/include/newtype/concepts.hpp +++ b/source/lib/include/newtype/concepts.hpp @@ -9,6 +9,71 @@ namespace nt::concepts { + inline namespace arithmetic + { + + template + concept addable = requires(SubjectType lhs, SubjectType rhs) { + { + lhs + rhs + } -> std::same_as; + }; + + template + concept nothrow_addable = requires(SubjectType lhs, SubjectType rhs) { + requires addable; + { + lhs + rhs + } noexcept; + }; + + template + concept divisible = requires(SubjectType lhs, SubjectType rhs) { + { + lhs / rhs + } -> std::same_as; + }; + + template + concept nothrow_divisible = requires(SubjectType lhs, SubjectType rhs) { + requires divisible; + { + lhs / rhs + } noexcept; + }; + + template + concept multipliable = requires(SubjectType lhs, SubjectType rhs) { + { + lhs * rhs + } -> std::same_as; + }; + + template + concept nothrow_multipliable = requires(SubjectType lhs, SubjectType rhs) { + requires multipliable; + { + lhs / rhs + } noexcept; + }; + + template + concept subtractable = requires(SubjectType lhs, SubjectType rhs) { + { + lhs - rhs + } -> std::same_as; + }; + + template + concept nothrow_subtractable = requires(SubjectType lhs, SubjectType rhs) { + requires subtractable; + { + lhs - rhs + } noexcept; + }; + + } // namespace arithmetic + inline namespace comparability { diff --git a/source/lib/include/newtype/impl/type_traits_extensions.hpp b/source/lib/include/newtype/impl/type_traits_extensions.hpp index c2caf51..3bf2c8d 100644 --- a/source/lib/include/newtype/impl/type_traits_extensions.hpp +++ b/source/lib/include/newtype/impl/type_traits_extensions.hpp @@ -11,119 +11,6 @@ namespace nt::impl { - inline namespace arithmetic - { - - template - struct is_addable : std::false_type - { - }; - - template - struct is_addable() + std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_addable_v = is_addable::value; - - template - struct is_nothrow_addable : std::false_type - { - }; - - template - struct is_nothrow_addable() + std::declval())>> - : std::bool_constant() + std::declval())> - { - }; - - template - auto constexpr is_nothrow_addable_v = is_nothrow_addable::value; - - template - struct is_subtractable : std::false_type - { - }; - - template - struct is_subtractable() - std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_subtractable_v = is_subtractable::value; - - template - struct is_nothrow_subtractable : std::false_type - { - }; - - template - struct is_nothrow_subtractable() - std::declval())>> - : std::bool_constant() - std::declval())> - { - }; - - template - auto constexpr is_nothrow_subtractable_v = is_nothrow_subtractable::value; - - template - struct is_multipliable : std::false_type - { - }; - - template - struct is_multipliable() * std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_multipliable_v = is_multipliable::value; - - template - struct is_nothrow_multipliable : std::false_type - { - }; - - template - struct is_nothrow_multipliable() * std::declval())>> - : std::bool_constant() * std::declval())> - { - }; - - template - auto constexpr is_nothrow_multipliable_v = is_nothrow_multipliable::value; - - template - struct is_dividable : std::false_type - { - }; - - template - struct is_dividable() / std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_dividable_v = is_dividable::value; - - template - struct is_nothrow_dividable : std::false_type - { - }; - - template - struct is_nothrow_dividable() / std::declval())>> - : std::bool_constant() / std::declval())> - { - }; - - template - auto constexpr is_nothrow_dividable_v = is_nothrow_dividable::value; - - } // namespace arithmetic - inline namespace compound_arithmetic { diff --git a/source/lib/include/newtype/newtype.hpp b/source/lib/include/newtype/newtype.hpp index 44bd1b8..4642122 100644 --- a/source/lib/include/newtype/newtype.hpp +++ b/source/lib/include/newtype/newtype.hpp @@ -353,11 +353,11 @@ namespace nt return input >> target.m_value; } - template + template auto DerivationClause> auto constexpr operator+(new_type const & lhs, new_type const & rhs) noexcept( - impl::is_nothrow_addable_v && std::is_nothrow_copy_constructible_v) - -> std::enable_if_t, new_type> + nt::concepts::nothrow_addable && std::is_nothrow_copy_constructible_v) + -> new_type { return {lhs.decay() + rhs.decay()}; } @@ -372,11 +372,11 @@ namespace nt return lhs; } - template + template auto DerivationClause> auto constexpr operator-(new_type const & lhs, new_type const & rhs) noexcept( - impl::is_nothrow_subtractable_v && std::is_nothrow_copy_constructible_v) - -> std::enable_if_t, new_type> + nt::concepts::nothrow_subtractable && std::is_nothrow_copy_constructible_v) + -> new_type { return {lhs.decay() - rhs.decay()}; } @@ -392,11 +392,11 @@ namespace nt return lhs; } - template + template auto DerivationClause> auto constexpr operator*(new_type const & lhs, new_type const & rhs) noexcept( - impl::is_nothrow_multipliable_v && std::is_nothrow_copy_constructible_v) - -> std::enable_if_t, new_type> + nt::concepts::nothrow_multipliable && std::is_nothrow_copy_constructible_v) + -> new_type { return {lhs.decay() * rhs.decay()}; } @@ -412,11 +412,11 @@ namespace nt return lhs; } - template + template auto DerivationClause> auto constexpr operator/(new_type const & lhs, new_type const & rhs) noexcept( - impl::is_nothrow_dividable_v && std::is_nothrow_copy_constructible_v) - -> std::enable_if_t, new_type> + nt::concepts::nothrow_divisible && std::is_nothrow_copy_constructible_v) + -> new_type { return {lhs.decay() / rhs.decay()}; } diff --git a/source/tests/src/arithmetic.cpp b/source/tests/src/arithmetic.cpp index 09b0c4a..647e600 100644 --- a/source/tests/src/arithmetic.cpp +++ b/source/tests/src/arithmetic.cpp @@ -23,7 +23,7 @@ SCENARIO("Addition", "[arithmetic]") THEN("it is not addable") { - STATIC_REQUIRE(!nt::impl::is_addable_v); + STATIC_REQUIRE(!nt::concepts::addable); } } @@ -33,7 +33,7 @@ SCENARIO("Addition", "[arithmetic]") THEN("it is addable") { - STATIC_REQUIRE(nt::impl::is_addable_v); + STATIC_REQUIRE(nt::concepts::addable); } } @@ -43,7 +43,7 @@ SCENARIO("Addition", "[arithmetic]") THEN("it is not addable") { - STATIC_REQUIRE(!nt::impl::is_addable_v == nt::impl::is_addable_v); + STATIC_REQUIRE(!nt::concepts::addable == nt::concepts::addable); } } @@ -53,7 +53,7 @@ SCENARIO("Addition", "[arithmetic]") THEN("it is addable") { - STATIC_REQUIRE(nt::impl::is_addable_v == nt::impl::is_addable_v); + STATIC_REQUIRE(nt::concepts::addable == nt::concepts::addable); } } @@ -96,7 +96,7 @@ SCENARIO("Subtraction", "[arithmetic]") THEN("it is not subtractable") { - STATIC_REQUIRE(!nt::impl::is_subtractable_v); + STATIC_REQUIRE(!nt::concepts::subtractable); } } @@ -106,7 +106,7 @@ SCENARIO("Subtraction", "[arithmetic]") THEN("it is subtractable") { - STATIC_REQUIRE(nt::impl::is_subtractable_v); + STATIC_REQUIRE(nt::concepts::subtractable); } } @@ -116,7 +116,7 @@ SCENARIO("Subtraction", "[arithmetic]") THEN("it is not addable") { - STATIC_REQUIRE(!nt::impl::is_subtractable_v); + STATIC_REQUIRE(!nt::concepts::subtractable); } } @@ -126,7 +126,7 @@ SCENARIO("Subtraction", "[arithmetic]") THEN("it is subtractable") { - STATIC_REQUIRE(nt::impl::is_subtractable_v); + STATIC_REQUIRE(nt::concepts::subtractable); } } @@ -169,7 +169,7 @@ SCENARIO("Multiplication", "[arithmetic]") THEN("it is not multipliable") { - STATIC_REQUIRE(!nt::impl::is_multipliable_v); + STATIC_REQUIRE(!nt::concepts::multipliable); } } @@ -179,7 +179,7 @@ SCENARIO("Multiplication", "[arithmetic]") THEN("it is multipliable") { - STATIC_REQUIRE(nt::impl::is_multipliable_v); + STATIC_REQUIRE(nt::concepts::multipliable); } } @@ -189,7 +189,7 @@ SCENARIO("Multiplication", "[arithmetic]") THEN("it is not multipliable") { - STATIC_REQUIRE(!nt::impl::is_multipliable_v); + STATIC_REQUIRE(!nt::concepts::multipliable); } } @@ -199,7 +199,7 @@ SCENARIO("Multiplication", "[arithmetic]") THEN("it is multipliable") { - STATIC_REQUIRE(nt::impl::is_multipliable_v); + STATIC_REQUIRE(nt::concepts::multipliable); } } @@ -242,7 +242,7 @@ SCENARIO("Division", "[arithmetic]") THEN("it is not divisible") { - STATIC_REQUIRE(!nt::impl::is_dividable_v); + STATIC_REQUIRE(!nt::concepts::divisible); } } @@ -252,7 +252,7 @@ SCENARIO("Division", "[arithmetic]") THEN("it is divisible") { - STATIC_REQUIRE(nt::impl::is_dividable_v); + STATIC_REQUIRE(nt::concepts::divisible); } } @@ -262,7 +262,7 @@ SCENARIO("Division", "[arithmetic]") THEN("it is not divisible") { - STATIC_REQUIRE(!nt::impl::is_dividable_v); + STATIC_REQUIRE(!nt::concepts::divisible); } } @@ -272,7 +272,7 @@ SCENARIO("Division", "[arithmetic]") THEN("it is divisible") { - STATIC_REQUIRE(nt::impl::is_dividable_v); + STATIC_REQUIRE(nt::concepts::divisible); } } -- cgit v1.2.3