diff options
author | babenko <babenko@yandex-team.com> | 2023-12-13 17:24:39 +0300 |
---|---|---|
committer | babenko <babenko@yandex-team.com> | 2023-12-13 19:27:29 +0300 |
commit | 296bfa05cb0fd7a1dbf4e3ae37623896131a8d1a (patch) | |
tree | d655d13489afbcaa68512d6c121c7c76a48d0fed | |
parent | a2a641f053c08c20145d1bdc14e685bcd295162a (diff) | |
download | ydb-296bfa05cb0fd7a1dbf4e3ae37623896131a8d1a.tar.gz |
Better relational operators for TStrongTypedef
-rw-r--r-- | library/cpp/yt/misc/strong_typedef-inl.h | 42 | ||||
-rw-r--r-- | library/cpp/yt/misc/strong_typedef.h | 21 |
2 files changed, 32 insertions, 31 deletions
diff --git a/library/cpp/yt/misc/strong_typedef-inl.h b/library/cpp/yt/misc/strong_typedef-inl.h index d24c76c1dd..0a8a9751f0 100644 --- a/library/cpp/yt/misc/strong_typedef-inl.h +++ b/library/cpp/yt/misc/strong_typedef-inl.h @@ -62,33 +62,27 @@ constexpr T&& TStrongTypedef<T, TTag>::Underlying() && return std::move(Underlying_); } -template <class T, class TTag> -constexpr bool TStrongTypedef<T, TTag>::operator==(const TStrongTypedef& rhs) const - noexcept(noexcept(Underlying_ == rhs.Underlying_)) - requires std::equality_comparable<T> -{ - //! NB: We add a constexpr branch to keep constexprness of the function - //! without making extra specializations explicitly. - if constexpr (std::same_as<T, void>) { - return true; +#define XX(op, defaultValue) \ + template <class T, class TTag> \ + constexpr auto TStrongTypedef<T, TTag>::operator op(const TStrongTypedef& rhs) const \ + noexcept(noexcept(Underlying_ op rhs.Underlying_)) \ + requires requires (T lhs, T rhs) {lhs op rhs; } \ + { \ + if constexpr (std::same_as<T, void>) { \ + return defaultValue; \ + } \ + return Underlying_ op rhs.Underlying_; \ } - return Underlying_ == rhs.Underlying_; -} +XX(<, false) +XX(>, false) +XX(<=, true) +XX(>=, true) +XX(==, true) +XX(!=, false) +XX(<=>, std::strong_ordering::equal) -template <class T, class TTag> -constexpr auto TStrongTypedef<T, TTag>::operator<=>(const TStrongTypedef& rhs) const - noexcept(noexcept(Underlying_ <=> rhs.Underlying_)) - requires std::three_way_comparable<T> -{ - //! NB: We add a constexpr branch to keep constexprness of the function - //! without making extra specializations explicitly. - if constexpr (std::same_as<T, void>) { - return std::strong_ordering::equal; - } - - return Underlying_ <=> rhs.Underlying_; -} +#undef XX template <class T, class TTag> TStrongTypedef<T, TTag>::operator bool() const diff --git a/library/cpp/yt/misc/strong_typedef.h b/library/cpp/yt/misc/strong_typedef.h index 3b093026a4..d47581701d 100644 --- a/library/cpp/yt/misc/strong_typedef.h +++ b/library/cpp/yt/misc/strong_typedef.h @@ -32,13 +32,20 @@ public: constexpr explicit operator const T&() const; constexpr explicit operator T&(); - constexpr bool operator==(const TStrongTypedef& rhs) const - noexcept(noexcept(Underlying_ == rhs.Underlying_)) - requires std::equality_comparable<T>; - - constexpr auto operator<=>(const TStrongTypedef& rhs) const - noexcept(noexcept(Underlying_ <=> rhs.Underlying_)) - requires std::three_way_comparable<T>; + #define XX(op) \ + constexpr auto operator op(const TStrongTypedef& rhs) const \ + noexcept(noexcept(Underlying_ op rhs.Underlying_)) \ + requires requires(T lhs, T rhs) {lhs op rhs; }; + + XX(<) + XX(>) + XX(<=) + XX(>=) + XX(==) + XX(!=) + XX(<=>) + + #undef XX explicit operator bool() const noexcept(noexcept(static_cast<bool>(Underlying_))); |