From 306982e16c4191604840e33b20db9479de977bbb Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 9 Jun 2023 11:30:03 +0200 Subject: concepts: replace compound arithmetic traits --- source/lib/include/newtype/concepts.hpp | 63 ++++++++++ .../newtype/impl/type_traits_extensions.hpp | 129 --------------------- source/lib/include/newtype/newtype.hpp | 63 +++++----- source/tests/src/arithmetic.cpp | 1 - source/tests/src/hash.cpp | 1 - source/tests/src/iterable.cpp | 1 - 6 files changed, 91 insertions(+), 167 deletions(-) delete mode 100644 source/lib/include/newtype/impl/type_traits_extensions.hpp diff --git a/source/lib/include/newtype/concepts.hpp b/source/lib/include/newtype/concepts.hpp index fc625ed..88313b9 100644 --- a/source/lib/include/newtype/concepts.hpp +++ b/source/lib/include/newtype/concepts.hpp @@ -169,6 +169,69 @@ namespace nt::concepts } // namespace comparability + inline namespace compound_arithmetic + { + template + concept compound_addable = requires(SubjectType & lhs, SubjectType const & rhs) { + { + lhs += rhs + } -> std::convertible_to; + }; + + template + concept nothrow_compound_addable = requires(SubjectType & lhs, SubjectType const & rhs) { + requires compound_addable; + { + lhs += rhs + } noexcept; + }; + + template + concept compound_divisible = requires(SubjectType & lhs, SubjectType const & rhs) { + { + lhs /= rhs + } -> std::convertible_to; + }; + + template + concept nothrow_compound_divisible = requires(SubjectType & lhs, SubjectType const & rhs) { + requires compound_divisible; + { + lhs /= rhs + } noexcept; + }; + + template + concept compound_multipliable = requires(SubjectType & lhs, SubjectType const & rhs) { + { + lhs *= rhs + } -> std::convertible_to; + }; + + template + concept nothrow_compound_multipliable = requires(SubjectType & lhs, SubjectType const & rhs) { + requires compound_multipliable; + { + lhs *= rhs + } noexcept; + }; + + template + concept compound_subtractable = requires(SubjectType & lhs, SubjectType const & rhs) { + { + lhs -= rhs + } -> std::convertible_to; + }; + + template + concept nothrow_compound_subtractable = requires(SubjectType & lhs, SubjectType const & rhs) { + requires compound_subtractable; + { + lhs -= rhs + } noexcept; + }; + } // namespace compound_arithmetic + inline namespace iostreamable { diff --git a/source/lib/include/newtype/impl/type_traits_extensions.hpp b/source/lib/include/newtype/impl/type_traits_extensions.hpp deleted file mode 100644 index dd25905..0000000 --- a/source/lib/include/newtype/impl/type_traits_extensions.hpp +++ /dev/null @@ -1,129 +0,0 @@ -#ifndef NEWTYPE_IMPL_TYPE_TRAITS_EXTENSIONS_HPP -#define NEWTYPE_IMPL_TYPE_TRAITS_EXTENSIONS_HPP - -#include "newtype/version.hpp" - -#include -#include -#include -#include - -namespace nt::impl -{ - - inline namespace compound_arithmetic - { - - template - struct is_add_assignable : std::false_type - { - }; - - template - struct is_add_assignable() += std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_add_assignable_v = is_add_assignable::value; - - template - struct is_nothrow_add_assignable : std::false_type - { - }; - - template - struct is_nothrow_add_assignable() += std::declval())>> - : std::bool_constant() += std::declval())> - { - }; - - template - auto constexpr is_nothrow_add_assignable_v = is_nothrow_add_assignable::value; - - template - struct is_subtract_assignable : std::false_type - { - }; - - template - struct is_subtract_assignable() -= std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_subtract_assignable_v = is_subtract_assignable::value; - - template - struct is_nothrow_subtract_assignable : std::false_type - { - }; - - template - struct is_nothrow_subtract_assignable() -= std::declval())>> - : std::bool_constant() -= std::declval())> - { - }; - - template - auto constexpr is_nothrow_subtract_assignable_v = is_nothrow_subtract_assignable::value; - - template - struct is_multiply_assignable : std::false_type - { - }; - - template - struct is_multiply_assignable() *= std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_multiply_assignable_v = is_multiply_assignable::value; - - template - struct is_nothrow_multiply_assignable : std::false_type - { - }; - - template - struct is_nothrow_multiply_assignable() *= std::declval())>> - : std::bool_constant() *= std::declval())> - { - }; - - template - auto constexpr is_nothrow_multiply_assignable_v = is_nothrow_multiply_assignable::value; - - template - struct is_divide_assignable : std::false_type - { - }; - - template - struct is_divide_assignable() /= std::declval())>> : std::true_type - { - }; - - template - auto constexpr is_divide_assignable_v = is_divide_assignable::value; - - template - struct is_nothrow_divide_assignable : std::false_type - { - }; - - template - struct is_nothrow_divide_assignable() /= std::declval())>> - : std::bool_constant() /= std::declval())> - { - }; - - template - auto constexpr is_nothrow_divide_assignable_v = is_nothrow_divide_assignable::value; - - } // namespace compound_arithmetic - -} // namespace nt::impl - -#endif \ No newline at end of file diff --git a/source/lib/include/newtype/newtype.hpp b/source/lib/include/newtype/newtype.hpp index b5eca97..824a3da 100644 --- a/source/lib/include/newtype/newtype.hpp +++ b/source/lib/include/newtype/newtype.hpp @@ -6,7 +6,6 @@ #include "newtype/deriving.hpp" #include "newtype/impl/new_type_iterator_types.hpp" #include "newtype/impl/new_type_storage.hpp" -#include "newtype/impl/type_traits_extensions.hpp" #include "newtype/version.hpp" #include @@ -33,33 +32,29 @@ namespace nt auto friend operator>>(std::basic_istream &, new_type &) noexcept( nt::concepts::nothrow_input_streamable) -> std::basic_istream &; - template + template auto DerivationClauseV> auto constexpr friend operator+=(new_type & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_add_assignable_v) - -> std::enable_if_t, - new_type &>; + new_type const & rhs) noexcept(nt::concepts::nothrow_compound_addable) + -> new_type &; - template + template auto DerivationClauseV> auto constexpr friend operator-=(new_type & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_subtract_assignable_v) - -> std::enable_if_t, - new_type &>; + new_type const & rhs) noexcept(nt::concepts::nothrow_compound_subtractable) + -> new_type &; - template + template auto DerivationClauseV> auto constexpr friend operator*=(new_type & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_multiply_assignable_v) - -> std::enable_if_t, - new_type &>; + new_type const & rhs) noexcept(nt::concepts::nothrow_compound_multipliable) + -> new_type &; - template + template auto DerivationClauseV> auto constexpr friend operator/=(new_type & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_divide_assignable_v) - -> std::enable_if_t, - new_type &>; + new_type const & rhs) noexcept(nt::concepts::nothrow_compound_divisible) + -> new_type &; template auto DerivationClauseV> auto constexpr friend begin(new_type & obj) -> @@ -338,11 +333,11 @@ namespace nt return {lhs.decay() + rhs.decay()}; } - template - auto constexpr operator+=(new_type & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_add_assignable_v) - -> std::enable_if_t, - new_type &> + template auto DerivationClause> + auto constexpr + operator+=(new_type & lhs, + new_type const & rhs) noexcept(nt::concepts::nothrow_compound_addable) + -> new_type & { lhs.m_value += rhs.m_value; return lhs; @@ -357,12 +352,11 @@ namespace nt return {lhs.decay() - rhs.decay()}; } - template + template auto DerivationClause> auto constexpr operator-=(new_type & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_subtract_assignable_v) - -> std::enable_if_t, - new_type &> + new_type const & rhs) noexcept(nt::concepts::nothrow_compound_subtractable) + -> new_type & { lhs.m_value -= rhs.m_value; return lhs; @@ -377,12 +371,11 @@ namespace nt return {lhs.decay() * rhs.decay()}; } - template + template auto DerivationClause> auto constexpr operator*=(new_type & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_multiply_assignable_v) - -> std::enable_if_t, - new_type &> + new_type const & rhs) noexcept(nt::concepts::nothrow_compound_multipliable) + -> new_type & { lhs.m_value *= rhs.m_value; return lhs; @@ -397,11 +390,11 @@ namespace nt return {lhs.decay() / rhs.decay()}; } - template - auto constexpr operator/=(new_type & lhs, - new_type const & rhs) noexcept(impl::is_nothrow_divide_assignable_v) - -> std::enable_if_t, - new_type &> + template auto DerivationClause> + auto constexpr + operator/=(new_type & lhs, + new_type const & rhs) noexcept(nt::concepts::nothrow_compound_divisible) + -> new_type & { lhs.m_value /= rhs.m_value; return lhs; diff --git a/source/tests/src/arithmetic.cpp b/source/tests/src/arithmetic.cpp index 647e600..8161f0c 100644 --- a/source/tests/src/arithmetic.cpp +++ b/source/tests/src/arithmetic.cpp @@ -1,6 +1,5 @@ #include "newtype/derivable.hpp" #include "newtype/deriving.hpp" -#include "newtype/impl/type_traits_extensions.hpp" #include "newtype/newtype.hpp" #include diff --git a/source/tests/src/hash.cpp b/source/tests/src/hash.cpp index bcf793c..9bf6862 100644 --- a/source/tests/src/hash.cpp +++ b/source/tests/src/hash.cpp @@ -1,7 +1,6 @@ #include "newtype/concepts.hpp" #include "newtype/derivable.hpp" #include "newtype/deriving.hpp" -#include "newtype/impl/type_traits_extensions.hpp" #include "newtype/newtype.hpp" #include diff --git a/source/tests/src/iterable.cpp b/source/tests/src/iterable.cpp index ba91500..bc862cc 100644 --- a/source/tests/src/iterable.cpp +++ b/source/tests/src/iterable.cpp @@ -1,6 +1,5 @@ #include "newtype/derivable.hpp" #include "newtype/deriving.hpp" -#include "newtype/impl/type_traits_extensions.hpp" #include "newtype/newtype.hpp" #include -- cgit v1.2.3