From 350a6e397f544d3c5444cbfd79d672b34cfd268c Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Jan 2020 15:36:00 +0100 Subject: new_type: implement subtract-assign --- doc/src/index.rst | 9 + include/newtype/impl/type_traits_extensions.hpp | 242 ++++++------------------ include/newtype/new_type.hpp | 29 ++- test/src/arithmetic_suite.cpp | 28 ++- 4 files changed, 123 insertions(+), 185 deletions(-) diff --git a/doc/src/index.rst b/doc/src/index.rst index 2694dba..591a168 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -302,6 +302,15 @@ Arithmetic Operators **enablement:** This operator shall be available iff. a) :cpp:type:`new_type::base_type` is subtractable using the operator :literal:`-` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. +.. cpp:function:: template \ + constexpr new_type & operator-=(new_type const & lhs, new_type const & rhs) + + **noexcept specification:** This operator shall be noexcept iff. :cpp:type:`new_type::base_type` is nothrow subtract-assignable. + + **enablement:** This operator shall be available iff. a) :cpp:type:`new_type::base_type` is subtract-assignable using the operator :literal:`-=` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. + .. cpp:function:: template \ diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index 8935a4e..5104306 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -826,188 +826,66 @@ namespace nt::impl template auto constexpr is_nothrow_add_assignable_v = is_nothrow_add_assignable::value; - // /** - // * @brief A trait to test if a given type is subtractable - // * - // * @tparam T The type to test - // * @note This specialization forms the base case for non-subtractable T - // */ - // template - // struct is_subtract_assignable : std::false_type - // { - // }; - - // /** - // * @brief A trait to test if a given type is subtractable - // * - // * @tparam T The type to test - // * @note This specialization forms the case for subtractable T - // */ - // template - // struct is_subtract_assignable() - std::declval())>> : std::true_type - // { - // }; - - // /** - // * @brief A variable template to test if a given type is subtractable - // * - // * @tparam T The type to test - // */ - // template - // auto constexpr is_subtractable_v = is_subtract_assignable::value; - - // /** - // * @brief A trait to test if a given type is noexcept subtractable - // * - // * @tparam T The type to test - // * @note This specialization forms the base case for non-noexcept subtractable or non-subtractable T - // */ - // template - // struct is_nothrow_subtractable : std::false_type - // { - // }; - - // /** - // * @brief A trait to test if a given type is noexcept subtractable - // * - // * @tparam T The type to test - // * @note This specialization forms the case for subtractable T detemining if T is noexcept subtractable - // */ - // template - // struct is_nothrow_subtractable() - std::declval())>> - // : std::bool_constant() - std::declval())> - // { - // }; - - // /** - // * @brief A variable template to test if a given type is noexcept subtractable - // * - // * @tparam T The type to test - // */ - // template - // auto constexpr is_nothrow_subtractable_v = is_nothrow_subtractable::value; - - // /** - // * @brief A trait to test if a given type is multipliable - // * - // * @tparam T The type to test - // * @note This specialization forms the base case for non-multipliable T - // */ - // template - // struct is_multipliable : std::false_type - // { - // }; - - // /** - // * @brief A trait to test if a given type is multipliable - // * - // * @tparam T The type to test - // * @note This specialization forms the case for multipliable T - // */ - // template - // struct is_multipliable() * std::declval())>> : std::true_type - // { - // }; - - // /** - // * @brief A variable template to test if a given type is multipliable - // * - // * @tparam T The type to test - // */ - // template - // auto constexpr is_multipliable_v = is_multipliable::value; - - // /** - // * @brief A trait to test if a given type is noexcept multipliable - // * - // * @tparam T The type to test - // * @note This specialization forms the base case for non-noexcept multipliable or non-multipliable T - // */ - // template - // struct is_nothrow_multipliable : std::false_type - // { - // }; - - // /** - // * @brief A trait to test if a given type is noexcept multipliable - // * - // * @tparam T The type to test - // * @note This specialization forms the case for multipliable T detemining if T is noexcept multipliable - // */ - // template - // struct is_nothrow_multipliable() * std::declval())>> - // : std::bool_constant() * std::declval())> - // { - // }; - - // /** - // * @brief A variable template to test if a given type is noexcept multipliable - // * - // * @tparam T The type to test - // */ - // template - // auto constexpr is_nothrow_multipliable_v = is_nothrow_multipliable::value; - - // /** - // * @brief A trait to test if a given type is dividable - // * - // * @tparam T The type to test - // * @note This specialization forms the base case for non-dividable T - // */ - // template - // struct is_dividable : std::false_type - // { - // }; - - // /** - // * @brief A trait to test if a given type is dividable - // * - // * @tparam T The type to test - // * @note This specialization forms the case for dividable T - // */ - // template - // struct is_dividable() / std::declval())>> : std::true_type - // { - // }; - - // /** - // * @brief A variable template to test if a given type is dividable - // * - // * @tparam T The type to test - // */ - // template - // auto constexpr is_dividable_v = is_dividable::value; - - // /** - // * @brief A trait to test if a given type is noexcept dividable - // * - // * @tparam T The type to test - // * @note This specialization forms the base case for non-noexcept dividable or non-dividable T - // */ - // template - // struct is_nothrow_dividable : std::false_type - // { - // }; - - // /** - // * @brief A trait to test if a given type is noexcept dividable - // * - // * @tparam T The type to test - // * @note This specialization forms the case for dividable T detemining if T is noexcept dividable - // */ - // template - // struct is_nothrow_dividable() / std::declval())>> - // : std::bool_constant() / std::declval())> - // { - // }; - - // /** - // * @brief A variable template to test if a given type is noexcept dividable - // * - // * @tparam T The type to test - // */ - // template - // auto constexpr is_nothrow_dividable_v = is_nothrow_dividable::value; + /** + * @brief A trait to test if a given type is subtract-assignable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-subtract-assignable T + */ + template + struct is_subtract_assignable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is subtract-assignable + * + * @tparam T The type to test + * @note This specialization forms the case for subtract-assignable T + */ + template + struct is_subtract_assignable() -= std::declval())>> : std::true_type + { + }; + + /** + * @brief A variable template to test if a given type is subtract-assignable + * + * @tparam T The type to test + */ + template + auto constexpr is_subtract_assignable_v = is_subtract_assignable::value; + + /** + * @brief A trait to test if a given type is noexcept subtract-assignable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-noexcept subtract-assignable or non-subtract-assignable T + */ + template + struct is_nothrow_subtract_assignable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is noexcept subtract-assignable + * + * @tparam T The type to test + * @note This specialization forms the case for subtract-assignable T detemining if T is noexcept subtract-assignable + */ + template + struct is_nothrow_subtract_assignable() -= std::declval())>> + : std::bool_constant() -= std::declval())> + { + }; + + /** + * @brief A variable template to test if a given type is noexcept subtract-assignable + * + * @tparam T The type to test + */ + template + auto constexpr is_nothrow_subtract_assignable_v = is_nothrow_subtract_assignable::value; } // namespace compound_arithmetic diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index 3f9fdea..8d55f61 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -43,6 +43,13 @@ namespace nt -> std::enable_if_t, new_type &>; + template + auto constexpr friend + operator-=(new_type & lhs, + new_type const & rhs) noexcept(impl::is_nothrow_subtract_assignable_v) + -> std::enable_if_t, + new_type &>; + using super = impl::new_type_move_assignment; public: @@ -368,7 +375,7 @@ namespace nt -> std::enable_if_t, new_type &> { - lhs.m_value += rhs.decay(); + lhs.m_value += rhs.m_value; return lhs; } @@ -390,6 +397,26 @@ namespace nt return {lhs.decay() - rhs.decay()}; } + /** + * @brief Subtract two instances of the same nt::new_type, modifying the left-hand side + * + * @note This operator is only available if the derivation clause of the passed in nt::new_type objects contains nt::Arithmetic and the base + * type is subtractable. + * @param lhs The left-hand side of the subtractition + * @param rhs The right-hand side of the subtractition + * @return a reference to the the modified value + */ + template + auto constexpr + operator-=(new_type & lhs, + new_type const & rhs) noexcept(impl::is_nothrow_subtract_assignable_v) + -> std::enable_if_t, + new_type &> + { + lhs.m_value -= rhs.m_value; + return lhs; + } + /** * @brief Multiply two instances of the same nt::new_type * diff --git a/test/src/arithmetic_suite.cpp b/test/src/arithmetic_suite.cpp index 7de76d6..75a7f46 100644 --- a/test/src/arithmetic_suite.cpp +++ b/test/src/arithmetic_suite.cpp @@ -222,12 +222,31 @@ inline namespace compound_addition ASSERT_EQUAL(elhs += erhs, (lhs += rhs).decay()); } + auto subtraction_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type() -> void + { + using type_alias = nt::new_type; + ASSERT((std::is_same_v() -= std::declval())>)); + } + + auto subtraction_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type() + -> void + { + using type_alias = nt::new_type; + auto elhs = 42; + auto erhs = 18; + auto lhs = type_alias{elhs}; + auto rhs = type_alias{erhs}; + ASSERT_EQUAL(elhs -= erhs, (lhs -= rhs).decay()); + } + } // namespace compound_addition auto arithmetic_suite() -> std::pair { return { { + // clang-format off + /// Addition Tests KAWAII(a_new__type_not_deriving_arithmetic_is_not_addable_with_instances_of_itself), KAWAII(a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself), @@ -262,8 +281,13 @@ auto arithmetic_suite() -> std::pair /// Compound Addition Tests KAWAII(addition_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), - KAWAII( - addition_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), + KAWAII(addition_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), + + /// Compound Addition Tests + KAWAII(subtraction_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), + KAWAII(subtraction_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), + + // clang-format on }, "Arithmetic Operators Tests"}; } \ No newline at end of file -- cgit v1.2.3