[PATCH] libstdc++: Implement P3060R3: Add std::views::indices(n)
Yuao Ma
addr2line@gmail.com
Tue Oct 14 15:12:37 GMT 2025
Hi Jonathan,
On Tue, Oct 14, 2025 at 10:55 PM Jonathan Wakely <jwakely@redhat.com> wrote:
> Attaching patches as application/octet-stream makes them hard to
> review and comment inline. If possible using 'git send-email' is the
> ideal way to submit patches. If that's not possible (it can be awkward
> to set up to send via gmail) then attaching a .txt file as text/plain
> makes things easier for reviewers.
>
I'm attaching it as a txt file now; hopefully, this works.
> Putting the [[nodiscard]] attribute after operator() was necessary in
> previous releases of GCC due to -fconcepts-ts compatibility, but
> that's no longer relevant for GCC 16. You can just put [[nodiscard]]
> before the return type.
>
Done.
> If you're contributing under the DCO terms then please don't add the
> FSF copyright notice to new tests. And the test is not very novel or
> interesting, so the licence text isn't needed either, see
> https://gcc.gnu.org/onlinedocs/libstdc++/manual/test.html#test.new_tests
>
Thanks for the guidance. I've removed the copyright now.
>
> Also, I think this can be unconditionally noexcept. We know that
> constructing iota_view from an integer-like type will not throw.
>
> For an arbitrary program-defined type that models weakly_incrementable
> it could throw, but not for integer-like types.
>
Done.
Thanks for the prompt review!
Yuao
-------------- next part --------------
From 275098e5dff81fc5e062b021305a7a55410f9028 Mon Sep 17 00:00:00 2001
From: Yuao Ma <c8ef@outlook.com>
Date: Tue, 14 Oct 2025 23:10:23 +0800
Subject: [PATCH] libstdc++: Implement P3060R3: Add std::views::indices(n)
This patch adds the views::indices function using iota.
libstdc++-v3/ChangeLog:
* include/bits/version.def: Add ranges_indices FTM.
* include/bits/version.h: Regenerate.
* include/std/ranges: Implement views::indices.
* testsuite/std/ranges/indices/1.cc: New test.
---
libstdc++-v3/include/bits/version.def | 8 ++++
libstdc++-v3/include/bits/version.h | 10 +++++
libstdc++-v3/include/std/ranges | 13 +++++++
.../testsuite/std/ranges/indices/1.cc | 39 +++++++++++++++++++
4 files changed, 70 insertions(+)
create mode 100644 libstdc++-v3/testsuite/std/ranges/indices/1.cc
diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def
index 1118da4b541..80cfea39fb3 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -1754,6 +1754,14 @@ ftms = {
};
};
+ftms = {
+ name = ranges_indices;
+ values = {
+ v = 202506;
+ cxxmin = 26;
+ };
+};
+
ftms = {
name = constexpr_bitset;
values = {
diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h
index c452bbeec8e..07a8ecabb6e 100644
--- a/libstdc++-v3/include/bits/version.h
+++ b/libstdc++-v3/include/bits/version.h
@@ -1956,6 +1956,16 @@
#endif /* !defined(__cpp_lib_ranges_starts_ends_with) */
#undef __glibcxx_want_ranges_starts_ends_with
+#if !defined(__cpp_lib_ranges_indices)
+# if (__cplusplus > 202302L)
+# define __glibcxx_ranges_indices 202506L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_ranges_indices)
+# define __cpp_lib_ranges_indices 202506L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_ranges_indices) */
+#undef __glibcxx_want_ranges_indices
+
#if !defined(__cpp_lib_constexpr_bitset)
# if (__cplusplus >= 202100L) && _GLIBCXX_HOSTED && (__cpp_constexpr_dynamic_alloc)
# define __glibcxx_constexpr_bitset 202202L
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index fd290ea362c..ec31df41f40 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -65,6 +65,7 @@
#define __glibcxx_want_ranges_chunk
#define __glibcxx_want_ranges_chunk_by
#define __glibcxx_want_ranges_enumerate
+#define __glibcxx_want_ranges_indices
#define __glibcxx_want_ranges_join_with
#define __glibcxx_want_ranges_repeat
#define __glibcxx_want_ranges_slide
@@ -785,6 +786,18 @@ namespace views
};
inline constexpr _Iota iota{};
+
+#ifdef __cpp_lib_ranges_indices // C++ >= 26
+ struct _Indices
+ {
+ template<ranges::__detail::__is_integer_like _Tp>
+ [[nodiscard]] constexpr auto
+ operator() (_Tp&& __e) const noexcept
+ { return iota(_Tp{}, __e); }
+ };
+
+ inline constexpr _Indices indices{};
+#endif // __cpp_lib_ranges_indices
} // namespace views
#if _GLIBCXX_HOSTED
diff --git a/libstdc++-v3/testsuite/std/ranges/indices/1.cc b/libstdc++-v3/testsuite/std/ranges/indices/1.cc
new file mode 100644
index 00000000000..8d4ae2775fb
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/ranges/indices/1.cc
@@ -0,0 +1,39 @@
+// { dg-do run { target c++26 } }
+
+#include <ranges>
+#include <vector>
+
+#include <testsuite_hooks.h>
+
+constexpr bool test() {
+ {
+ auto indices_view = std::ranges::views::indices(4);
+ static_assert(std::ranges::range<decltype(indices_view)>);
+
+ VERIFY(indices_view.size() == 4);
+ VERIFY(indices_view[0] == 0);
+ VERIFY(indices_view[1] == 1);
+ VERIFY(indices_view[2] == 2);
+ VERIFY(indices_view[3] == 3);
+ }
+
+ {
+ std::vector v(4, 0);
+
+ auto indices_view = std::ranges::views::indices(std::ranges::size(v));
+ static_assert(std::ranges::range<decltype(indices_view)>);
+
+ VERIFY(indices_view.size() == 4);
+ VERIFY(indices_view[0] == 0);
+ VERIFY(indices_view[1] == 1);
+ VERIFY(indices_view[2] == 2);
+ VERIFY(indices_view[3] == 3);
+ }
+
+ return true;
+}
+
+int main() {
+ VERIFY(test());
+ static_assert(test());
+}
--
2.43.0
More information about the Libstdc++
mailing list