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.
};
};
+ftms = {
+ name = ranges_indices;
+ values = {
+ v = 202506;
+ cxxmin = 26;
+ };
+};
+
ftms = {
name = constexpr_bitset;
values = {
#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
#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
};
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
--- /dev/null
+// { dg-do run { target c++26 } }
+
+#include <testsuite_hooks.h>
+
+#include <ranges>
+#include <type_traits>
+#include <vector>
+
+template <typename T>
+constexpr bool test(T n) {
+ auto indices_view = std::ranges::views::indices(n);
+ static_assert(
+ std::is_same_v<T, std::ranges::range_value_t<decltype(indices_view)>>);
+ static_assert(noexcept(std::ranges::views::indices(n)));
+
+ VERIFY(indices_view.size() == n);
+ for (T i = 0; i < n; ++i) VERIFY(indices_view[i] == i);
+
+ return true;
+}
+
+int main() {
+ VERIFY(test<int>(41));
+ static_assert(test<int>(41));
+ VERIFY(test<short>(42));
+ static_assert(test<short>(42));
+ VERIFY(test<long>(43));
+ static_assert(test<long>(43));
+ VERIFY(test<size_t>(44));
+ static_assert(test<size_t>(44));
+}