From: Yuao Ma Date: Tue, 14 Oct 2025 16:28:48 +0000 (+0800) Subject: libstdc++: Implement P3060R3: Add std::views::indices(n) X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7aefb48bb036c5189bea44fc0d8680812a08ed32;p=thirdparty%2Fgcc.git 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. --- diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index f60a518b28d..04232187965 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 cbd82ff81e8..df7a291b05f 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..25d2e28e72f 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 + [[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..805b29e358f --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/indices/1.cc @@ -0,0 +1,31 @@ +// { dg-do run { target c++26 } } + +#include + +#include +#include +#include + +template +constexpr bool test(T n) { + auto indices_view = std::ranges::views::indices(n); + static_assert( + std::is_same_v>); + 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(41)); + static_assert(test(41)); + VERIFY(test(42)); + static_assert(test(42)); + VERIFY(test(43)); + static_assert(test(43)); + VERIFY(test(44)); + static_assert(test(44)); +}