From 6998183b580b60ac72f79b3ebc4f206fa35937ac Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Jan 2020 15:46:27 +0100 Subject: new_type: implement divide-assignment --- include/newtype/impl/type_traits_extensions.hpp | 61 +++++++++++++++++++++++++ include/newtype/new_type.hpp | 30 +++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index 6224d29..10dbc07 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -948,6 +948,67 @@ namespace nt::impl template auto constexpr is_nothrow_multiply_assignable_v = is_nothrow_multiply_assignable::value; + /** + * @brief A trait to test if a given type is divide-assignable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-divide-assignable T + */ + template + struct is_divide_assignable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is divide-assignable + * + * @tparam T The type to test + * @note This specialization forms the case for divide-assignable T + */ + template + struct is_divide_assignable() /= std::declval())>> : std::true_type + { + }; + + /** + * @brief A variable template to test if a given type is divide-assignable + * + * @tparam T The type to test + */ + template + auto constexpr is_divide_assignable_v = is_divide_assignable::value; + + /** + * @brief A trait to test if a given type is noexcept divide-assignable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-noexcept divide-assignable or non-divide-assignable T + */ + template + struct is_nothrow_divide_assignable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is noexcept divide-assignable + * + * @tparam T The type to test + * @note This specialization forms the case for divide-assignable T detemining if T is noexcept divide-assignable + */ + template + struct is_nothrow_divide_assignable() /= std::declval())>> + : std::bool_constant() /= std::declval())> + { + }; + + /** + * @brief A variable template to test if a given type is noexcept divide-assignable + * + * @tparam T The type to test + */ + template + auto constexpr is_nothrow_divide_assignable_v = is_nothrow_divide_assignable::value; + } // namespace compound_arithmetic } // namespace nt::impl diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index a7ab4af..d22732a 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -57,6 +57,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_divide_assignable_v) + -> std::enable_if_t, + new_type &>; + using super = impl::new_type_move_assignment; public: @@ -447,8 +454,8 @@ namespace nt * * @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 multiplyable. - * @param lhs The left-hand side of the multiplyition - * @param rhs The right-hand side of the multiplyition + * @param lhs The left-hand side of the multiplication + * @param rhs The right-hand side of the multiplication * @return a reference to the the modified value */ template @@ -480,6 +487,25 @@ namespace nt return {lhs.decay() / rhs.decay()}; } + /** + * @brief Divide 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 dividable. + * @param lhs The left-hand side of the division + * @param rhs The right-hand side of the division + * @return a reference to the the modified value + */ + template + auto constexpr operator/=(new_type & lhs, + new_type const & rhs) noexcept(impl::is_nothrow_divide_assignable_v) + -> std::enable_if_t, + new_type &> + { + lhs.m_value /= rhs.m_value; + return lhs; + } + } // namespace nt #endif -- cgit v1.2.3