public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-1228] libstdc++: Implement LWG 3403 for std::ranges::ssize
@ 2021-06-04 20:43 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-06-04 20:43 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:621ea10ca060ba19ec693aa73b5e29d553cca849

commit r12-1228-g621ea10ca060ba19ec693aa73b5e29d553cca849
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Jun 4 20:28:04 2021 +0100

    libstdc++: Implement LWG 3403 for std::ranges::ssize
    
    I already changed the constraints for ranges::ssize to use ranges::size,
    this implements the rest of LWG 3403, so that the returned type is the
    signed type corresponding to the result of ranges::size.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/ranges_base.h (_SSize): Return the result of
            ranges::size converted to the wider of make-signed-like-t<S> and
            ptrdiff_t, rather than the ranges different type.
            * testsuite/std/ranges/access/ssize.cc: Adjust expected result
            for an iota_view that uses an integer class type for its
            difference_type.

Diff:
---
 libstdc++-v3/include/bits/ranges_base.h           | 22 ++++++++++++++++------
 libstdc++-v3/testsuite/std/ranges/access/ssize.cc |  9 ++++++---
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h
index 61d91eb8389..e3c3962bcd9 100644
--- a/libstdc++-v3/include/bits/ranges_base.h
+++ b/libstdc++-v3/include/bits/ranges_base.h
@@ -425,22 +425,32 @@ namespace ranges
 
     struct _SSize
     {
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // 3403. Domain of ranges::ssize(E) doesn't match ranges::size(E)
       template<typename _Tp>
 	requires requires (_Tp& __t) { _Size{}(__t); }
 	constexpr auto
 	operator()(_Tp&& __t) const noexcept(noexcept(_Size{}(__t)))
 	{
-	  using __iter_type = decltype(_Begin{}(__t));
-	  using __diff_type = iter_difference_t<__iter_type>;
-	  using __gnu_cxx::__int_traits;
 	  auto __size = _Size{}(__t);
-	  if constexpr (integral<__diff_type>)
+	  using __size_type = decltype(__size);
+	  // Return the wider of ptrdiff_t and make-signed-like-t<__size_type>.
+	  if constexpr (integral<__size_type>)
 	    {
-	      if constexpr (__int_traits<__diff_type>::__digits
+	      using __gnu_cxx::__int_traits;
+	      if constexpr (__int_traits<__size_type>::__digits
 			    < __int_traits<ptrdiff_t>::__digits)
 		return static_cast<ptrdiff_t>(__size);
+	      else
+		return static_cast<make_signed_t<__size_type>>(__size);
 	    }
-	  return static_cast<__diff_type>(__size);
+#if defined __STRICT_ANSI__ && defined __SIZEOF_INT128__
+	  // For strict-ansi modes integral<__int128> is false
+	  else if constexpr (__detail::__is_int128<__size_type>)
+	    return static_cast<unsigned __int128>(__size);
+#endif
+	  else // Must be one of __max_diff_type or __max_size_type.
+	    return __detail::__max_diff_type(__size);
 	}
     };
 
diff --git a/libstdc++-v3/testsuite/std/ranges/access/ssize.cc b/libstdc++-v3/testsuite/std/ranges/access/ssize.cc
index b00ec5db1f8..f53f462580c 100644
--- a/libstdc++-v3/testsuite/std/ranges/access/ssize.cc
+++ b/libstdc++-v3/testsuite/std/ranges/access/ssize.cc
@@ -76,9 +76,12 @@ void
 test06()
 {
   auto i = std::views::iota(1ull, 5u);
-  auto s = std::ranges::ssize(i);
-  using R = std::ranges::range_difference_t<decltype(i)>;
-  static_assert( std::same_as<decltype(s), R> );
+  auto s = std::ranges::size(i);
+  auto ss = std::ranges::ssize(i);
+  // std::ranges::range_difference_t<decltype(i)> is larger than long long,
+  // but LWG 3403 says ranges::ssize(i) returns the signed version of the
+  // type that ranges::size(i) returns, not the range's difference_type.
+  static_assert( std::same_as<decltype(ss), std::make_signed_t<decltype(s)>> );
   VERIFY( s == 4 );
 }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-06-04 20:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-04 20:43 [gcc r12-1228] libstdc++: Implement LWG 3403 for std::ranges::ssize Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).