From 9a5d11d4c5701c8ad2e6aa1213cc9fd2937ccbc4 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Jan 2020 13:15:53 +0100 Subject: new_type: implement add-assign --- include/newtype/impl/type_traits_extensions.hpp | 249 ++++++++++++++++++++++++ include/newtype/new_type.hpp | 26 +++ 2 files changed, 275 insertions(+) (limited to 'include') diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index f9cd51a..8935a4e 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -762,6 +762,255 @@ namespace nt::impl } // namespace arithmetic + inline namespace compound_arithmetic + { + + /** + * @brief A trait to test if a given type is add-assignable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-add-assignable T + */ + template + struct is_add_assignable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is add-assignable + * + * @tparam T The type to test + * @note This specialization forms the case for add-assignable T + */ + template + struct is_add_assignable() += std::declval())>> : std::true_type + { + }; + + /** + * @brief A variable template to test if a given type is add-assignable + * + * @tparam T The type to test + */ + template + auto constexpr is_add_assignable_v = is_add_assignable::value; + + /** + * @brief A trait to test if a given type is noexcept add-assignable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-noexcept add-assignable or non-add-assignable T + */ + template + struct is_nothrow_add_assignable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is noexcept add-assignable + * + * @tparam T The type to test + * @note This specialization forms the case for add-assignable T detemining if T is noexcept add-assignable + */ + template + struct is_nothrow_add_assignable() += std::declval())>> + : std::bool_constant() += std::declval())> + { + }; + + /** + * @brief A variable template to test if a given type is noexcept add-assignable + * + * @tparam T The type to test + */ + 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; + + } // namespace compound_arithmetic + } // namespace nt::impl #endif \ No newline at end of file diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index 59e0b1a..3f9fdea 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -36,6 +36,13 @@ namespace nt -> std::enable_if_t, BaseTypeT>, std::basic_istream> &; + template + auto constexpr friend + operator+=(new_type & lhs, + new_type const & rhs) noexcept(impl::is_nothrow_add_assignable_v) + -> std::enable_if_t, + new_type &>; + using super = impl::new_type_move_assignment; public: @@ -346,6 +353,25 @@ namespace nt return {lhs.decay() + rhs.decay()}; } + /** + * @brief Add 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 addable. + * @param lhs The left-hand side of the addition + * @param rhs The right-hand side of the addition + * @return a reference to the the modified value + */ + template + auto constexpr operator+=(new_type & lhs, + new_type const & rhs) noexcept(impl::is_nothrow_add_assignable_v) + -> std::enable_if_t, + new_type &> + { + lhs.m_value += rhs.decay(); + return lhs; + } + /** * @brief Subtract two instances of the same nt::new_type * -- cgit v1.2.3