From b196e2c10ed8823c681623dfd7bc09b0eb3ad1d2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Jan 2020 22:38:18 +0100 Subject: new_type: implement support for base comparison --- include/newtype/new_type.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'include') 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 @@ -228,6 +228,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 + auto constexpr operator==(new_type const & lhs, + BaseType const & rhs) noexcept(impl::is_nothrow_equality_comparable_v) + -> std::enable_if_t, 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 + auto constexpr + operator==(BaseType const & lhs, + new_type const & rhs) noexcept(impl::is_nothrow_equality_comparable_v) + -> std::enable_if_t, bool> + { + return lhs == rhs.decay(); + } + /** * @brief Compare two objects for non-equality * @@ -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 + auto constexpr operator!=(new_type const & lhs, + BaseType const & rhs) noexcept(impl::is_nothrow_inequality_comparable_v) + -> std::enable_if_t, 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 + auto constexpr + operator!=(BaseType const & lhs, + new_type const & rhs) noexcept(impl::is_nothrow_inequality_comparable_v) + -> std::enable_if_t, bool> + { + return lhs != rhs.decay(); + } + /// @section Relational operators /** -- cgit v1.2.3