diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2020-01-02 22:38:18 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2020-01-02 22:39:49 +0100 |
| commit | b196e2c10ed8823c681623dfd7bc09b0eb3ad1d2 (patch) | |
| tree | 68a0670dba2ac95f71189383ce202e08345d9373 /include | |
| parent | 6998183b580b60ac72f79b3ebc4f206fa35937ac (diff) | |
| download | newtype-b196e2c10ed8823c681623dfd7bc09b0eb3ad1d2.tar.xz newtype-b196e2c10ed8823c681623dfd7bc09b0eb3ad1d2.zip | |
new_type: implement support for base comparison
Diffstat (limited to 'include')
| -rw-r--r-- | include/newtype/new_type.hpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index d22732a..b599c45 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -229,6 +229,37 @@ namespace nt } /** + * @brief Compare an instance of a given nt::new_type with an object of its base type + * + * @throw This comparison operator throws any exception thrown by the base type comparison operator. It it noexcept iff. the base type + * comparison operator is noexcept. + * @return true iff. the base type comparison operator returns true, false otherwise. + */ + template<typename BaseType, typename TagType, auto DerivationClause> + auto constexpr operator==(new_type<BaseType, TagType, DerivationClause> const & lhs, + BaseType const & rhs) noexcept(impl::is_nothrow_equality_comparable_v<BaseType>) + -> std::enable_if_t<DerivationClause(nt::EqBase) && impl::is_equality_comparable_v<BaseType>, bool> + { + return lhs.decay() == rhs; + } + + /** + * @brief Compare an instance of the base type with an instance of a given nt::new_type + * + * @throw This comparison operator throws any exception thrown by the base type comparison operator. It it noexcept iff. the base type + * comparison operator is noexcept. + * @return true iff. the base type comparison operator returns true, false otherwise. + */ + template<typename BaseType, typename TagType, auto DerivationClause> + auto constexpr + operator==(BaseType const & lhs, + new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(impl::is_nothrow_equality_comparable_v<BaseType>) + -> std::enable_if_t<DerivationClause(nt::EqBase) && impl::is_equality_comparable_v<BaseType>, bool> + { + return lhs == rhs.decay(); + } + + /** * @brief Compare two objects for non-equality * * @throw This comparison operator throws any exception thrown by the base type comparison operator. It it noexcept iff. the base type @@ -244,6 +275,37 @@ namespace nt return lhs.decay() != rhs.decay(); } + /** + * @brief Compare an instance of a given nt::new_type with an object of its base type + * + * @throw This comparison operator throws any exception thrown by the base type comparison operator. It it noexcept iff. the base type + * comparison operator is noexcept. + * @return true iff. the base type comparison operator returns true, false otherwise. + */ + template<typename BaseType, typename TagType, auto DerivationClause> + auto constexpr operator!=(new_type<BaseType, TagType, DerivationClause> const & lhs, + BaseType const & rhs) noexcept(impl::is_nothrow_inequality_comparable_v<BaseType>) + -> std::enable_if_t<DerivationClause(nt::EqBase) && impl::is_inequality_comparable_v<BaseType>, bool> + { + return lhs.decay() != rhs; + } + + /** + * @brief Compare an instance of the base type with an instance of a given nt::new_type + * + * @throw This comparison operator throws any exception thrown by the base type comparison operator. It it noexcept iff. the base type + * comparison operator is noexcept. + * @return true iff. the base type comparison operator returns true, false otherwise. + */ + template<typename BaseType, typename TagType, auto DerivationClause> + auto constexpr + operator!=(BaseType const & lhs, + new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(impl::is_nothrow_inequality_comparable_v<BaseType>) + -> std::enable_if_t<DerivationClause(nt::EqBase) && impl::is_inequality_comparable_v<BaseType>, bool> + { + return lhs != rhs.decay(); + } + /// @section Relational operators /** |
