diff options
| -rw-r--r-- | doc/src/index.rst | 16 | ||||
| -rw-r--r-- | include/newtype/impl/type_traits_extensions.hpp | 62 | ||||
| -rw-r--r-- | include/newtype/new_type.hpp | 18 | ||||
| -rw-r--r-- | test/src/arithmetic_suite.cpp | 47 |
4 files changed, 140 insertions, 3 deletions
diff --git a/doc/src/index.rst b/doc/src/index.rst index 18e81f7..e8647c1 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -280,7 +280,7 @@ Arithmetic Operators auto DerivationClause> \ constexpr new_type<BaseType, TagType, DerivationClause> operator+(new_type<BaseType, TagType, DerivationClause> const & lhs, new_type<BaseType, TagType, DerivationClause> const & rhs) - **noexcept specification:** This input operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow addable as well as nothrow copy-constructible. + **noexcept specification:** This operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow addable as well as nothrow copy-constructible. **enablement:** This operator shall be available iff. a) :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is addable using the operator :literal:`+` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. @@ -289,7 +289,7 @@ Arithmetic Operators auto DerivationClause> \ constexpr new_type<BaseType, TagType, DerivationClause> operator-(new_type<BaseType, TagType, DerivationClause> const & lhs, new_type<BaseType, TagType, DerivationClause> const & rhs) - **noexcept specification:** This input operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow subtractable as well as nothrow copy-constructible. + **noexcept specification:** This operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow subtractable as well as nothrow copy-constructible. **enablement:** This operator shall be available iff. a) :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is subtractable using the operator :literal:`-` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. @@ -298,10 +298,20 @@ Arithmetic Operators auto DerivationClause> \ constexpr new_type<BaseType, TagType, DerivationClause> operator*(new_type<BaseType, TagType, DerivationClause> const & lhs, new_type<BaseType, TagType, DerivationClause> const & rhs) - **noexcept specification:** This input operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow multipliable as well as nothrow copy-constructible. + **noexcept specification:** This operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow multipliable as well as nothrow copy-constructible. **enablement:** This operator shall be available iff. a) :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is multipliable using the operator :literal:`*` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. + +.. cpp:function:: template<typename BaseType, \ + typename TagType, \ + auto DerivationClause> \ + constexpr new_type<BaseType, TagType, DerivationClause> operator/(new_type<BaseType, TagType, DerivationClause> const & lhs, new_type<BaseType, TagType, DerivationClause> const & rhs) + + **noexcept specification:** This operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow dividable as well as nothrow copy-constructible. + + **enablement:** This operator shall be available iff. a) :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is dividable using the operator :literal:`/` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. + Header :literal:`<newtype/derivable.hpp>` ========================================= 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<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; + } // 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<typename BaseType, typename TagType, auto DerivationClause> + auto constexpr + operator/(new_type<BaseType, TagType, DerivationClause> const & lhs, new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept( + impl::is_nothrow_dividable_v<BaseType> && std::is_nothrow_copy_constructible_v<BaseType>) + -> std::enable_if_t<DerivationClause(nt::Arithmetic) && impl::is_dividable_v<BaseType>, new_type<BaseType, TagType, DerivationClause>> + { + return {lhs.decay() / rhs.decay()}; + } + } // namespace nt #endif diff --git a/test/src/arithmetic_suite.cpp b/test/src/arithmetic_suite.cpp index e77c1dd..7c2b617 100644 --- a/test/src/arithmetic_suite.cpp +++ b/test/src/arithmetic_suite.cpp @@ -163,6 +163,45 @@ inline namespace multiplication_tests } // namespace multiplication_tests +inline namespace division_tests +{ + + auto a_new__type_not_deriving_arithmetic_is_not_dividable_with_instances_of_itself() -> void + { + using type_alias = nt::new_type<int, struct tag>; + ASSERT(!(nt::impl::is_dividable_v<type_alias>)); + } + + auto a_new__type_deriving_arithmetic_is_dividable_with_instances_of_itself() -> void + { + using type_alias = nt::new_type<int, struct tag, deriving(nt::Arithmetic)>; + ASSERT(nt::impl::is_dividable_v<type_alias>); + } + + template<typename T> + auto a_new__type_deriving_arithmetic_is_dividable_with_instances_of_itself_if_the_base_type_is_dividable() -> void + { + static_assert(nt::impl::is_dividable_v<T>, "Sanity Check"); + using type_alias = nt::new_type<T, struct tag, deriving(nt::Arithmetic)>; + ASSERT_EQUAL(nt::impl::is_dividable_v<T>, nt::impl::is_dividable_v<type_alias>); + } + + auto division_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type() -> void + { + using type_alias = nt::new_type<int, struct tag, deriving(nt::Arithmetic)>; + ASSERT((std::is_same_v<type_alias, decltype(std::declval<type_alias const &>() / std::declval<type_alias const &>())>)); + } + + auto division_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type() -> void + { + using type_alias = nt::new_type<int, struct tag, deriving(nt::Arithmetic)>; + auto lhs = type_alias{24}; + auto rhs = type_alias{2}; + ASSERT_EQUAL(24 / 2, (lhs / rhs).decay()); + } + +} // namespace division_tests + auto arithmetic_suite() -> std::pair<cute::suite, std::string> { return { @@ -190,6 +229,14 @@ auto arithmetic_suite() -> std::pair<cute::suite, std::string> KAWAII(a_new__type_deriving_arithmetic_is_multipliable_with_instances_of_itself_if_the_base_type_is_multipliable<multipliable_type>), KAWAII(multiplication_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), KAWAII(multiplication_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), + + /// Division Tests + KAWAII(a_new__type_not_deriving_arithmetic_is_not_dividable_with_instances_of_itself), + KAWAII(a_new__type_deriving_arithmetic_is_dividable_with_instances_of_itself), + KAWAII(a_new__type_deriving_arithmetic_is_dividable_with_instances_of_itself_if_the_base_type_is_dividable<int>), + KAWAII(a_new__type_deriving_arithmetic_is_dividable_with_instances_of_itself_if_the_base_type_is_dividable<dividable_type>), + KAWAII(division_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), + KAWAII(division_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), }, "Arithmetic Operators Tests"}; }
\ No newline at end of file |
