From 9c95106af2fbaa9f5f8f605ee7df54cc90356df6 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 31 Dec 2019 16:54:49 +0100 Subject: new_type: implement addition via nt::Arithmetic --- include/newtype/impl/type_traits_extensions.hpp | 66 +++++++++++++++++++++++++ include/newtype/new_type.hpp | 21 ++++++++ 2 files changed, 87 insertions(+) (limited to 'include') diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index ae0c7d5..d6dbfd6 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -513,6 +513,72 @@ namespace nt::impl } // namespace iostreamable + inline namespace arithmetic + { + + /** + * @brief A trait to test if a given type is addable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-addable T + */ + template + struct is_addable : 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 addable T + */ + template + struct is_addable() + std::declval())>> : std::true_type + { + }; + + /** + * @brief A variable template to test if a given type is addable + * + * @tparam T The type to test + */ + template + auto constexpr is_addable_v = is_addable::value; + + /** + * @brief A trait to test if a given type is noexcept addable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-noexcept addable or non-addable T + */ + template + struct is_nothrow_addable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is noexcept addable + * + * @tparam T The type to test + * @note This specialization forms the case for addable T detemining if T is noexcept addable + */ + template + struct is_nothrow_addable() + std::declval())>> + : std::bool_constant() + std::declval())> + { + }; + + /** + * @brief A variable template to test if a given type is noexcept addable + * + * @tparam T The type to test + */ + template + auto constexpr is_nothrow_addable_v = is_nothrow_addable::value; + + } // namespace arithmetic + } // namespace nt::impl #endif \ No newline at end of file diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index 77b531e..8228a94 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -325,6 +325,27 @@ namespace nt { return input >> target.m_value; } + + /// @section Arithmetic operators + + /** + * @brief Add 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 addable. + * @param lhs The left-hand side of the addition + * @param rhs The right-hand side of the addition + * @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_addable_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