diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/newtype/impl/type_traits_extensions.hpp | 242 | ||||
| -rw-r--r-- | include/newtype/new_type.hpp | 29 |
2 files changed, 88 insertions, 183 deletions
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<typename T> auto constexpr is_nothrow_add_assignable_v = is_nothrow_add_assignable<T>::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<typename T, typename = void> - // 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<typename T> - // struct is_subtract_assignable<T, std::void_t<decltype(std::declval<T const &>() - std::declval<T const &>())>> : std::true_type - // { - // }; - - // /** - // * @brief A variable template to test if a given type is subtractable - // * - // * @tparam T The type to test - // */ - // template<typename T> - // auto constexpr is_subtractable_v = is_subtract_assignable<T>::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<typename T, typename = void> - // 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<typename T> - // struct is_nothrow_subtractable<T, std::void_t<decltype(std::declval<T const &>() - std::declval<T const &>())>> - // : std::bool_constant<noexcept(std::declval<T const &>() - std::declval<T const &>())> - // { - // }; - - // /** - // * @brief A variable template to test if a given type is noexcept subtractable - // * - // * @tparam T The type to test - // */ - // template<typename T> - // auto constexpr is_nothrow_subtractable_v = is_nothrow_subtractable<T>::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<typename T, typename = void> - // 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<typename T> - // struct is_multipliable<T, std::void_t<decltype(std::declval<T const &>() * std::declval<T const &>())>> : std::true_type - // { - // }; - - // /** - // * @brief A variable template to test if a given type is multipliable - // * - // * @tparam T The type to test - // */ - // template<typename T> - // auto constexpr is_multipliable_v = is_multipliable<T>::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<typename T, typename = void> - // 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<typename T> - // struct is_nothrow_multipliable<T, std::void_t<decltype(std::declval<T const &>() * std::declval<T const &>())>> - // : std::bool_constant<noexcept(std::declval<T const &>() * std::declval<T const &>())> - // { - // }; - - // /** - // * @brief A variable template to test if a given type is noexcept multipliable - // * - // * @tparam T The type to test - // */ - // template<typename T> - // auto constexpr is_nothrow_multipliable_v = is_nothrow_multipliable<T>::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<typename T, typename = void> - // 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<typename T> - // struct is_dividable<T, std::void_t<decltype(std::declval<T const &>() / std::declval<T const &>())>> : std::true_type - // { - // }; - - // /** - // * @brief A variable template to test if a given type is dividable - // * - // * @tparam T The type to test - // */ - // template<typename T> - // auto constexpr is_dividable_v = is_dividable<T>::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<typename T, typename = void> - // 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<typename T> - // struct is_nothrow_dividable<T, std::void_t<decltype(std::declval<T const &>() / std::declval<T const &>())>> - // : std::bool_constant<noexcept(std::declval<T const &>() / std::declval<T const &>())> - // { - // }; - - // /** - // * @brief A variable template to test if a given type is noexcept dividable - // * - // * @tparam T The type to test - // */ - // template<typename T> - // auto constexpr is_nothrow_dividable_v = is_nothrow_dividable<T>::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<typename T, typename = void> + 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<typename T> + struct is_subtract_assignable<T, std::void_t<decltype(std::declval<T &>() -= std::declval<T const &>())>> : std::true_type + { + }; + + /** + * @brief A variable template to test if a given type is subtract-assignable + * + * @tparam T The type to test + */ + template<typename T> + auto constexpr is_subtract_assignable_v = is_subtract_assignable<T>::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<typename T, typename = void> + 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<typename T> + struct is_nothrow_subtract_assignable<T, std::void_t<decltype(std::declval<T &>() -= std::declval<T const &>())>> + : std::bool_constant<noexcept(std::declval<T &>() -= std::declval<T const &>())> + { + }; + + /** + * @brief A variable template to test if a given type is noexcept subtract-assignable + * + * @tparam T The type to test + */ + template<typename T> + auto constexpr is_nothrow_subtract_assignable_v = is_nothrow_subtract_assignable<T>::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<DerivationClauseV(nt::Arithmetic) && impl::is_add_assignable_v<BaseTypeT>, new_type<BaseTypeT, TagTypeT, DerivationClauseV> &>; + template<typename BaseTypeT, typename TagTypeT, auto DerivationClauseV> + auto constexpr friend + operator-=(new_type<BaseTypeT, TagTypeT, DerivationClauseV> & lhs, + new_type<BaseTypeT, TagTypeT, DerivationClauseV> const & rhs) noexcept(impl::is_nothrow_subtract_assignable_v<BaseTypeT>) + -> std::enable_if_t<DerivationClauseV(nt::Arithmetic) && impl::is_subtract_assignable_v<BaseTypeT>, + new_type<BaseTypeT, TagTypeT, DerivationClauseV> &>; + using super = impl::new_type_move_assignment<BaseType, TagType>; public: @@ -368,7 +375,7 @@ namespace nt -> std::enable_if_t<DerivationClause(nt::Arithmetic) && impl::is_add_assignable_v<BaseType>, new_type<BaseType, TagType, DerivationClause> &> { - lhs.m_value += rhs.decay(); + lhs.m_value += rhs.m_value; return lhs; } @@ -391,6 +398,26 @@ namespace nt } /** + * @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<typename BaseType, typename TagType, auto DerivationClause> + auto constexpr + operator-=(new_type<BaseType, TagType, DerivationClause> & lhs, + new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(impl::is_nothrow_subtract_assignable_v<BaseType>) + -> std::enable_if_t<DerivationClause(nt::Arithmetic) && impl::is_subtract_assignable_v<BaseType>, + new_type<BaseType, TagType, DerivationClause> &> + { + lhs.m_value -= rhs.m_value; + return lhs; + } + + /** * @brief Multiply two instances of the same nt::new_type * * @note This operator is only available if the derivation clause of the passed in nt::new_type objects contains nt::Arithmetic and the base |
