diff options
author | Patrick Palka <ppalka@redhat.com> | 2021-03-15 10:31:45 -0400 |
---|---|---|
committer | Patrick Palka <ppalka@redhat.com> | 2021-03-15 10:31:45 -0400 |
commit | dce586ff83e5bf86956cb24e646c7b9a21283ce4 (patch) | |
tree | bda38ab3ea0d336f441ad1c8d4ce229571bad175 | |
parent | 33f36b34ac20d410d07ff4d144fc8165efcc11ef (diff) | |
download | gcc-dce586ff83e5bf86956cb24e646c7b9a21283ce4.zip gcc-dce586ff83e5bf86956cb24e646c7b9a21283ce4.tar.gz gcc-dce586ff83e5bf86956cb24e646c7b9a21283ce4.tar.bz2 |
libstdc++: Implement missing operator overloads in max_size_type.h
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.
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.
-rw-r--r-- | libstdc++-v3/include/bits/max_size_type.h | 78 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/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 05984a3..27d6379 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 @@ -91,6 +92,30 @@ namespace ranges { 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 { const auto __sum = _M_val + __r._M_val; @@ -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 @@ -437,6 +474,30 @@ namespace ranges { 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 { _M_rep += __r._M_rep; @@ -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 9284d71..983bdfb 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> |