diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/newtype/impl/type_traits_extensions.hpp | 60 | ||||
| -rw-r--r-- | include/newtype/new_type.hpp | 18 |
2 files changed, 78 insertions, 0 deletions
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<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; } // 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<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_multipliable_v<BaseType> && std::is_nothrow_copy_constructible_v<BaseType>) + -> std::enable_if_t<DerivationClause(nt::Arithmetic) && impl::is_multipliable_v<BaseType>, new_type<BaseType, TagType, DerivationClause>> + { + return {lhs.decay() * rhs.decay()}; + } + } // namespace nt #endif |
