* [committed] libstdc++: Implement LWG 3392 for std::ranges::distance
@ 2021-10-01 19:43 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-10-01 19:43 UTC (permalink / raw)
To: libstdc++, gcc-patches
[-- 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 );
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-10-01 19:43 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-01 19:43 [committed] libstdc++: Implement LWG 3392 for std::ranges::distance 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).