public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Implement LWG 3392 for std::ranges::distance
Date: Fri, 1 Oct 2021 20:43:23 +0100	[thread overview]
Message-ID: <YVdk2yEF/Cpm0zSc@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 225 bytes --]

libstdc++-v3/ChangeLog:

	* include/bits/ranges_base.h (ranges::distance): Split overload
	into two (LWG 3392).
	* testsuite/24_iterators/range_operations/lwg3392.cc: New test.

Tested powerpc64le-linux. Committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2766 bytes --]

commit 20751fad19e1b0fb4272309dd6d7fde182b08dc1
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 1 14:22:38 2021

    libstdc++: Implement LWG 3392 for std::ranges::distance
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/ranges_base.h (ranges::distance): Split overload
            into two (LWG 3392).
            * testsuite/24_iterators/range_operations/lwg3392.cc: New test.

diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h
index d6166ab1dd3..01d0c35f4b4 100644
--- a/libstdc++-v3/include/bits/ranges_base.h
+++ b/libstdc++-v3/include/bits/ranges_base.h
@@ -787,22 +787,25 @@ namespace ranges
   struct __distance_fn final
   {
     template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
+      requires (!sized_sentinel_for<_Sent, _It>)
+      constexpr iter_difference_t<_It>
+      operator()[[nodiscard]](_It __first, _Sent __last) const
+      {
+	iter_difference_t<_It> __n = 0;
+	while (__first != __last)
+	  {
+	    ++__first;
+	    ++__n;
+	  }
+	return __n;
+      }
+
+    template<input_or_output_iterator _It, sized_sentinel_for<_It> _Sent>
       [[nodiscard]]
       constexpr iter_difference_t<_It>
-      operator()(_It __first, _Sent __last) const
+      operator()(const _It& __first, const _Sent& __last) const
       {
-	if constexpr (sized_sentinel_for<_Sent, _It>)
-	  return __last - __first;
-	else
-	  {
-	    iter_difference_t<_It> __n = 0;
-	    while (__first != __last)
-	      {
-		++__first;
-		++__n;
-	      }
-	    return __n;
-	  }
+	return __last - __first;
       }
 
     template<range _Range>
diff --git a/libstdc++-v3/testsuite/24_iterators/range_operations/lwg3392.cc b/libstdc++-v3/testsuite/24_iterators/range_operations/lwg3392.cc
new file mode 100644
index 00000000000..32780353512
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/range_operations/lwg3392.cc
@@ -0,0 +1,30 @@
+// { dg-options "-std=gnu++20" }
+// { dg-do compile { target c++20 } }
+
+#include <iterator>
+
+struct movable_iterator
+{
+  using difference_type = long;
+
+  movable_iterator() = default;
+  movable_iterator(movable_iterator&&) = default;
+  movable_iterator& operator=(movable_iterator&&) = default;
+
+  int operator*() const { return 1; }
+
+  movable_iterator& operator++() { return *this; }
+  void operator++(int) { }
+
+  bool operator==(const movable_iterator&) const = default;
+};
+
+using namespace std;
+
+constexpr counted_iterator<movable_iterator> it({}, 3);
+
+static_assert( sized_sentinel_for<std::default_sentinel_t, counted_iterator<movable_iterator>> );
+// LWG 3392
+// ranges::distance() cannot be used on a move-only iterator
+// with a sized sentinel
+static_assert( ranges::distance(it, default_sentinel) == 3 );

                 reply	other threads:[~2021-10-01 19:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YVdk2yEF/Cpm0zSc@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).