From b34cbb0fa7dcd783245d2d2b2352846579bbb77b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Jan 2020 12:56:56 +0100 Subject: new_type: implement division --- include/newtype/impl/type_traits_extensions.hpp | 62 +++++++++++++++++++++++++ include/newtype/new_type.hpp | 18 +++++++ 2 files changed, 80 insertions(+) (limited to 'include') diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index c547d02..f9cd51a 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -698,6 +698,68 @@ namespace nt::impl */ 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 arithmetic } // namespace nt::impl diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index 08d5024..59e0b1a 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -382,6 +382,24 @@ namespace nt return {lhs.decay() * rhs.decay()}; } + /** + * @brief Divide 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 dividable. + * @param lhs The left-hand side of the division + * @param rhs The right-hand side of the division + * @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_dividable_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