From 9c81cffc32a5e88ac9f81fe3d08c2a670efa79d2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Jan 2020 12:48:57 +0100 Subject: new_type: implement multiplication --- doc/src/index.rst | 9 ++++ include/newtype/impl/type_traits_extensions.hpp | 60 +++++++++++++++++++++++++ include/newtype/new_type.hpp | 18 ++++++++ test/src/arithmetic_suite.cpp | 49 +++++++++++++++++++- 4 files changed, 135 insertions(+), 1 deletion(-) diff --git a/doc/src/index.rst b/doc/src/index.rst index 7f75ec4..18e81f7 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -293,6 +293,15 @@ Arithmetic Operators **enablement:** This operator shall be available iff. a) :cpp:type:`new_type::base_type` is subtractable using the operator :literal:`-` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. +.. cpp:function:: template \ + constexpr new_type operator*(new_type const & lhs, new_type const & rhs) + + **noexcept specification:** This input operator shall be noexcept iff. :cpp:type:`new_type::base_type` is nothrow multipliable as well as nothrow copy-constructible. + + **enablement:** This operator shall be available iff. a) :cpp:type:`new_type::base_type` is multipliable using the operator :literal:`*` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. + Header :literal:`` ========================================= diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index e9bbefb..c547d02 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -638,6 +638,66 @@ namespace nt::impl 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; } // namespace arithmetic } // namespace nt::impl diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index bc8ecee..08d5024 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -364,6 +364,24 @@ namespace nt return {lhs.decay() - rhs.decay()}; } + /** + * @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 + * type is multipliable. + * @param lhs The left-hand side of the multiplication + * @param rhs The right-hand side of the multiplication + * @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_multipliable_v && std::is_nothrow_copy_constructible_v) + -> std::enable_if_t, new_type> + { + return {lhs.decay() * rhs.decay()}; + } + } // namespace nt #endif diff --git a/test/src/arithmetic_suite.cpp b/test/src/arithmetic_suite.cpp index b7713e0..e77c1dd 100644 --- a/test/src/arithmetic_suite.cpp +++ b/test/src/arithmetic_suite.cpp @@ -111,7 +111,7 @@ inline namespace subtraction_tests auto subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type() -> void { using type_alias = nt::new_type; - ASSERT((std::is_same_v() + std::declval())>)); + ASSERT((std::is_same_v() - std::declval())>)); } auto subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type() -> void @@ -124,6 +124,45 @@ inline namespace subtraction_tests } // namespace subtraction_tests +inline namespace multiplication_tests +{ + + auto a_new__type_not_deriving_arithmetic_is_not_multipliable_with_instances_of_itself() -> void + { + using type_alias = nt::new_type; + ASSERT(!(nt::impl::is_multipliable_v)); + } + + auto a_new__type_deriving_arithmetic_is_multipliable_with_instances_of_itself() -> void + { + using type_alias = nt::new_type; + ASSERT(nt::impl::is_multipliable_v); + } + + template + auto a_new__type_deriving_arithmetic_is_multipliable_with_instances_of_itself_if_the_base_type_is_multipliable() -> void + { + static_assert(nt::impl::is_multipliable_v, "Sanity Check"); + using type_alias = nt::new_type; + ASSERT_EQUAL(nt::impl::is_multipliable_v, nt::impl::is_multipliable_v); + } + + auto multiplication_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type() -> void + { + using type_alias = nt::new_type; + ASSERT((std::is_same_v() * std::declval())>)); + } + + auto multiplication_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; + auto lhs = type_alias{24}; + auto rhs = type_alias{18}; + ASSERT_EQUAL(24 * 18, (lhs * rhs).decay()); + } + +} // namespace multiplication_tests + auto arithmetic_suite() -> std::pair { return { @@ -143,6 +182,14 @@ auto arithmetic_suite() -> std::pair KAWAII(a_new__type_deriving_arithmetic_is_subtractable_with_instances_of_itself_if_the_base_type_is_subtractable), KAWAII(subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), KAWAII(subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), + + /// Multiplication Tests + KAWAII(a_new__type_not_deriving_arithmetic_is_not_multipliable_with_instances_of_itself), + KAWAII(a_new__type_deriving_arithmetic_is_multipliable_with_instances_of_itself), + KAWAII(a_new__type_deriving_arithmetic_is_multipliable_with_instances_of_itself_if_the_base_type_is_multipliable), + KAWAII(a_new__type_deriving_arithmetic_is_multipliable_with_instances_of_itself_if_the_base_type_is_multipliable), + 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), }, "Arithmetic Operators Tests"}; } \ No newline at end of file -- cgit v1.2.3