From de5d0cbb9c58852ff0038be5495fa7e5d656dd1d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Jan 2020 11:43:54 +0100 Subject: new_type: implement subtraction --- include/newtype/impl/type_traits_extensions.hpp | 61 +++++++++++++++++++++++++ include/newtype/new_type.hpp | 18 ++++++++ 2 files changed, 79 insertions(+) (limited to 'include') diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index d6dbfd6..56824e2 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -577,6 +577,67 @@ namespace nt::impl template auto constexpr is_nothrow_addable_v = is_nothrow_addable::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_subtractable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is input streamable + * + * @tparam T The type to test + * @note This specialization forms the case for subtractable T + */ + template + struct is_subtractable() + 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_subtractable::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; + } // namespace arithmetic } // namespace nt::impl diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index 8228a94..bc8ecee 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -346,6 +346,24 @@ namespace nt return {lhs.decay() + rhs.decay()}; } + /** + * @brief Subtract 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 + * type is subtractable. + * @param lhs The left-hand side of the subtraction + * @param rhs The right-hand side of the subtraction + * @return a new instance of the same nt::new_type + */ + template + 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> + { + return {lhs.decay() - rhs.decay()}; + } + } // namespace nt #endif -- cgit v1.2.3