This implements operator++, operator-- and operator<=> for the integer-class types defined in max_size_type.h, which I overlooked when originally implementing the class.
Tested on x86_64-pc-linux-gnu, does this look OK for trunk? libstdc++-v3/ChangeLog: * include/bits/max_size_type.h (__max_size_type::operator _Tp): Fix formatting. (__max_size_type::operator++): Define. (__max_size_type::operator--): Likewise. (__max_size_type::operator<=>): Conditionally define (in place of the other comparison operators). (__max_diff_type::operator _Tp): Fix formatting. (__max_diff_type::operator++): Define. (__max_diff_type::operator--): Likewise. (__max_diff_type::operator<=>): Conditionally define (in place of the other comparison operators). * testsuite/std/ranges/iota/max_size_type.cc (test01): Test these operator overloads. --- libstdc++-v3/include/bits/max_size_type.h | 78 ++++++++++++++++++- .../std/ranges/iota/max_size_type.cc | 36 +++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/max_size_type.h b/libstdc++-v3/include/bits/max_size_type.h index 05984a30f1d..27d63797c49 100644 --- a/libstdc++-v3/include/bits/max_size_type.h +++ b/libstdc++-v3/include/bits/max_size_type.h @@ -71,7 +71,8 @@ namespace ranges __max_size_type(const __max_diff_type& __d) noexcept; template<typename _Tp> requires integral<_Tp> || __is_int128<_Tp> - constexpr explicit operator _Tp() const noexcept + constexpr explicit + operator _Tp() const noexcept { return _M_val; } constexpr explicit @@ -90,6 +91,30 @@ namespace ranges operator-() const noexcept { return operator~() + 1; } + constexpr __max_size_type& + operator++() noexcept + { return *this += 1; } + + constexpr __max_size_type + operator++(int) noexcept + { + auto __tmp = *this; + ++*this; + return __tmp; + } + + constexpr __max_size_type& + operator--() noexcept + { return *this -= 1; } + + constexpr __max_size_type + operator--(int) noexcept + { + auto __tmp = *this; + --*this; + return __tmp; + } + constexpr __max_size_type& operator+=(const __max_size_type& __r) noexcept { @@ -355,6 +380,16 @@ namespace ranges operator==(const __max_size_type& __l, const __max_size_type& __r) noexcept { return __l._M_val == __r._M_val && __l._M_msb == __r._M_msb; } +#if __cpp_lib_three_way_comparison + friend constexpr strong_ordering + operator<=>(const __max_size_type& __l, const __max_size_type& __r) noexcept + { + if (__l._M_msb ^ __r._M_msb) + return __l._M_msb ? strong_ordering::greater : strong_ordering::less; + else + return __l._M_val <=> __r._M_val; + } +#else friend constexpr bool operator!=(const __max_size_type& __l, const __max_size_type& __r) noexcept { return !(__l == __r); } @@ -379,6 +414,7 @@ namespace ranges friend constexpr bool operator>=(const __max_size_type& __l, const __max_size_type& __r) noexcept { return __r <= __l; } +#endif #if __SIZEOF_INT128__ using __rep = unsigned __int128; @@ -417,7 +453,8 @@ namespace ranges { } template<typename _Tp> requires integral<_Tp> || __is_int128<_Tp> - constexpr explicit operator _Tp() const noexcept + constexpr explicit + operator _Tp() const noexcept { return static_cast<_Tp>(_M_rep); } constexpr explicit @@ -436,6 +473,30 @@ namespace ranges operator~() const noexcept { return __max_diff_type(~_M_rep); } + constexpr __max_diff_type& + operator++() noexcept + { return *this += 1; } + + constexpr __max_diff_type + operator++(int) noexcept + { + auto __tmp = *this; + ++*this; + return __tmp; + } + + constexpr __max_diff_type& + operator--() noexcept + { return *this -= 1; } + + constexpr __max_diff_type + operator--(int) noexcept + { + auto __tmp = *this; + --*this; + return __tmp; + } + constexpr __max_diff_type& operator+=(const __max_diff_type& __r) noexcept { @@ -647,6 +708,18 @@ namespace ranges operator==(const __max_diff_type& __l, const __max_diff_type& __r) noexcept { return __l._M_rep == __r._M_rep; } +#if __cpp_lib_three_way_comparison + constexpr strong_ordering + operator<=>(const __max_diff_type& __r) const noexcept + { + const auto __lsign = _M_rep._M_msb; + const auto __rsign = __r._M_rep._M_msb; + if (__lsign ^ __rsign) + return __lsign ? strong_ordering::less : strong_ordering::greater; + else + return _M_rep <=> __r._M_rep; + } +#else friend constexpr bool operator!=(const __max_diff_type& __l, const __max_diff_type& __r) noexcept { return !(__l == __r); } @@ -673,6 +746,7 @@ namespace ranges friend constexpr bool operator>=(const __max_diff_type& __l, const __max_diff_type& __r) noexcept { return !(__l < __r); } +#endif private: __max_size_type _M_rep = 0; diff --git a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc index 9284d71dbeb..983bdfbdaa6 100644 --- a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc +++ b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc @@ -145,6 +145,42 @@ test01() auto h = [] (auto a) { a <<= a; return a; }; static_assert(h(max_size_t(3)) == 24); static_assert(h(max_diff_t(3)) == 24); + + auto w = [] (auto a) { + const auto b = a; + VERIFY( a++ == b && a == b+1 ); + VERIFY( a-- == b+1 && a == b ); + + VERIFY( ++(++a) == b+2 ); + VERIFY( --(--a) == b ); + return true; + }; + static_assert(w(max_size_t(10))); + static_assert(w(-max_diff_t(10))); + +#if __cpp_lib_three_way_comparison + static_assert(max_size_t{1} <=> max_size_t{9} == std::strong_ordering::less); + static_assert(max_size_t{3} <=> max_size_t{2} == std::strong_ordering::greater); + static_assert(max_size_t{5} <=> max_size_t{5} == std::strong_ordering::equal); + static_assert(~max_size_t{1} <=> ~max_size_t{9} == std::strong_ordering::greater); + static_assert(~max_size_t{3} <=> ~max_size_t{2} == std::strong_ordering::less); + static_assert(~max_size_t{5} <=> ~max_size_t{5} == std::strong_ordering::equal); + static_assert(~max_size_t{5} <=> max_size_t{9} == std::strong_ordering::greater); + static_assert(~max_size_t{9} <=> max_size_t{5} == std::strong_ordering::greater); + static_assert(max_size_t{5} <=> ~max_size_t{9} == std::strong_ordering::less); + static_assert(max_size_t{9} <=> ~max_size_t{5} == std::strong_ordering::less); + + static_assert(max_diff_t{1} <=> max_diff_t{9} == std::strong_ordering::less); + static_assert(max_diff_t{3} <=> max_diff_t{2} == std::strong_ordering::greater); + static_assert(max_diff_t{5} <=> max_diff_t{5} == std::strong_ordering::equal); + static_assert(max_diff_t{-1} <=> max_diff_t{-9} == std::strong_ordering::greater); + static_assert(max_diff_t{-3} <=> max_diff_t{-2} == std::strong_ordering::less); + static_assert(max_diff_t{-5} <=> max_diff_t{-5} == std::strong_ordering::equal); + static_assert(max_diff_t{-5} <=> max_diff_t{9} == std::strong_ordering::less); + static_assert(max_diff_t{-9} <=> max_diff_t{5} == std::strong_ordering::less); + static_assert(max_diff_t{5} <=> max_diff_t{-9} == std::strong_ordering::greater); + static_assert(max_diff_t{9} <=> max_diff_t{-5} == std::strong_ordering::greater); +#endif } template<bool signed_p, bool shorten_p> -- 2.31.0.rc2