aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.vscode/settings.json3
-rw-r--r--doc/src/index.rst9
-rw-r--r--include/newtype/impl/type_traits_extensions.hpp249
-rw-r--r--include/newtype/new_type.hpp26
-rw-r--r--test/src/arithmetic_suite.cpp22
5 files changed, 308 insertions, 1 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json
index f869c8c..8caed1c 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -62,7 +62,8 @@
"typeinfo": "cpp",
"valarray": "cpp",
"*.ipp": "cpp",
- "bitset": "cpp"
+ "bitset": "cpp",
+ "netfwd": "cpp"
},
"cmake.configureArguments": "-DCMAKE_EXPORT_COMPILE_COMMANDS=YES",
"cmake.cpptools.intelliSenseMode": "gcc-x64",
diff --git a/doc/src/index.rst b/doc/src/index.rst
index e8647c1..2694dba 100644
--- a/doc/src/index.rst
+++ b/doc/src/index.rst
@@ -287,6 +287,15 @@ Arithmetic Operators
.. 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 add-assignable.
+
+ **enablement:** This operator shall be available iff. a) :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is add-assignable 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 subtractable as well as nothrow copy-constructible.
diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp
index f9cd51a..8935a4e 100644
--- a/include/newtype/impl/type_traits_extensions.hpp
+++ b/include/newtype/impl/type_traits_extensions.hpp
@@ -762,6 +762,255 @@ namespace nt::impl
} // namespace arithmetic
+ inline namespace compound_arithmetic
+ {
+
+ /**
+ * @brief A trait to test if a given type is add-assignable
+ *
+ * @tparam T The type to test
+ * @note This specialization forms the base case for non-add-assignable T
+ */
+ template<typename T, typename = void>
+ struct is_add_assignable : std::false_type
+ {
+ };
+
+ /**
+ * @brief A trait to test if a given type is add-assignable
+ *
+ * @tparam T The type to test
+ * @note This specialization forms the case for add-assignable T
+ */
+ template<typename T>
+ struct is_add_assignable<T, std::void_t<decltype(std::declval<T &>() += std::declval<T const &>())>> : std::true_type
+ {
+ };
+
+ /**
+ * @brief A variable template to test if a given type is add-assignable
+ *
+ * @tparam T The type to test
+ */
+ template<typename T>
+ auto constexpr is_add_assignable_v = is_add_assignable<T>::value;
+
+ /**
+ * @brief A trait to test if a given type is noexcept add-assignable
+ *
+ * @tparam T The type to test
+ * @note This specialization forms the base case for non-noexcept add-assignable or non-add-assignable T
+ */
+ template<typename T, typename = void>
+ struct is_nothrow_add_assignable : std::false_type
+ {
+ };
+
+ /**
+ * @brief A trait to test if a given type is noexcept add-assignable
+ *
+ * @tparam T The type to test
+ * @note This specialization forms the case for add-assignable T detemining if T is noexcept add-assignable
+ */
+ template<typename T>
+ struct is_nothrow_add_assignable<T, std::void_t<decltype(std::declval<T &>() += std::declval<T const &>())>>
+ : std::bool_constant<noexcept(std::declval<T &>() += std::declval<T const &>())>
+ {
+ };
+
+ /**
+ * @brief A variable template to test if a given type is noexcept add-assignable
+ *
+ * @tparam T The type to test
+ */
+ template<typename T>
+ auto constexpr is_nothrow_add_assignable_v = is_nothrow_add_assignable<T>::value;
+
+ // /**
+ // * @brief A trait to test if a given type is subtractable
+ // *
+ // * @tparam T The type to test
+ // * @note This specialization forms the base case for non-subtractable T
+ // */
+ // template<typename T, typename = void>
+ // struct is_subtract_assignable : std::false_type
+ // {
+ // };
+
+ // /**
+ // * @brief A trait to test if a given type is subtractable
+ // *
+ // * @tparam T The type to test
+ // * @note This specialization forms the case for subtractable T
+ // */
+ // template<typename T>
+ // struct is_subtract_assignable<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 subtractable
+ // *
+ // * @tparam T The type to test
+ // */
+ // template<typename T>
+ // auto constexpr is_subtractable_v = is_subtract_assignable<T>::value;
+
+ // /**
+ // * @brief A trait to test if a given type is noexcept subtractable
+ // *
+ // * @tparam T The type to test
+ // * @note This specialization forms the base case for non-noexcept subtractable or non-subtractable T
+ // */
+ // template<typename T, typename = void>
+ // struct is_nothrow_subtractable : std::false_type
+ // {
+ // };
+
+ // /**
+ // * @brief A trait to test if a given type is noexcept subtractable
+ // *
+ // * @tparam T The type to test
+ // * @note This specialization forms the case for subtractable T detemining if T is noexcept subtractable
+ // */
+ // template<typename T>
+ // struct is_nothrow_subtractable<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 subtractable
+ // *
+ // * @tparam T The type to test
+ // */
+ // 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;
+
+ // /**
+ // * @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 compound_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 59e0b1a..3f9fdea 100644
--- a/include/newtype/new_type.hpp
+++ b/include/newtype/new_type.hpp
@@ -36,6 +36,13 @@ namespace nt
-> std::enable_if_t<DerivationClauseV(nt::Read) && impl::is_input_streamable_v<std::basic_istream<CharType, StreamTraits>, BaseTypeT>,
std::basic_istream<CharType, StreamTraits>> &;
+ template<typename BaseTypeT, typename TagTypeT, auto DerivationClauseV>
+ auto constexpr friend
+ operator+=(new_type<BaseTypeT, TagTypeT, DerivationClauseV> & lhs,
+ new_type<BaseTypeT, TagTypeT, DerivationClauseV> const & rhs) noexcept(impl::is_nothrow_add_assignable_v<BaseTypeT>)
+ -> std::enable_if_t<DerivationClauseV(nt::Arithmetic) && impl::is_add_assignable_v<BaseTypeT>,
+ new_type<BaseTypeT, TagTypeT, DerivationClauseV> &>;
+
using super = impl::new_type_move_assignment<BaseType, TagType>;
public:
@@ -347,6 +354,25 @@ namespace nt
}
/**
+ * @brief Add two instances of the same nt::new_type, modifying the left-hand side
+ *
+ * @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 reference to the the modified value
+ */
+ template<typename BaseType, typename TagType, auto DerivationClause>
+ auto constexpr operator+=(new_type<BaseType, TagType, DerivationClause> & lhs,
+ new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(impl::is_nothrow_add_assignable_v<BaseType>)
+ -> std::enable_if_t<DerivationClause(nt::Arithmetic) && impl::is_add_assignable_v<BaseType>,
+ new_type<BaseType, TagType, DerivationClause> &>
+ {
+ lhs.m_value += rhs.decay();
+ return lhs;
+ }
+
+ /**
* @brief Subtract 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
diff --git a/test/src/arithmetic_suite.cpp b/test/src/arithmetic_suite.cpp
index 7c2b617..5d760d2 100644
--- a/test/src/arithmetic_suite.cpp
+++ b/test/src/arithmetic_suite.cpp
@@ -202,6 +202,28 @@ inline namespace division_tests
} // namespace division_tests
+inline namespace compound_addition
+{
+
+ auto addition_assignment_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 &>() += std::declval<type_alias const &>())>));
+ }
+
+ auto addition_assignment_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{18};
+ auto elhs = 42;
+ auto erhs = 18;
+ ASSERT_EQUAL(elhs += erhs, (lhs += rhs).decay());
+ }
+
+} // namespace compound_addition
+
auto arithmetic_suite() -> std::pair<cute::suite, std::string>
{
return {