https://gcc.gnu.org/g:437ed2fec78cbd72cd31922b7d506847e45c52f9
commit r16-4554-g437ed2fec78cbd72cd31922b7d506847e45c52f9 Author: Jonathan Wakely <[email protected]> Date: Tue Oct 21 21:21:45 2025 +0100 libstdc++: Add missing constraints to views::indices Calling views::indices(n) should be expression equivalent to views::iota(decltype(n)(0), n), which means it should have the same constraints as views::iota and be SFINAE-friendly. libstdc++-v3/ChangeLog: * include/std/ranges (indices::operator()): Constrain using __can_iota_view concept. * testsuite/std/ranges/indices/1.cc: Check SFINAE-friendliness required by expression equivalence. Replace unused <vector> header with <stddef.h> needed for size_t. Diff: --- libstdc++-v3/include/std/ranges | 1 + libstdc++-v3/testsuite/std/ranges/indices/1.cc | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 25d2e28e72ff..158692d92a70 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -791,6 +791,7 @@ namespace views struct _Indices { template<ranges::__detail::__is_integer_like _Tp> + requires __detail::__can_iota_view<_Tp> [[nodiscard]] constexpr auto operator() (_Tp __e) const noexcept { return iota(_Tp{}, __e); } diff --git a/libstdc++-v3/testsuite/std/ranges/indices/1.cc b/libstdc++-v3/testsuite/std/ranges/indices/1.cc index 805b29e358f7..038b38f0ce0d 100644 --- a/libstdc++-v3/testsuite/std/ranges/indices/1.cc +++ b/libstdc++-v3/testsuite/std/ranges/indices/1.cc @@ -4,7 +4,7 @@ #include <ranges> #include <type_traits> -#include <vector> +#include <stddef.h> template <typename T> constexpr bool test(T n) { @@ -29,3 +29,17 @@ int main() { VERIFY(test<size_t>(44)); static_assert(test<size_t>(44)); } + +template<typename T> +constexpr size_t test_wider(T n) +{ + // If indices(n) works, try again with ranges::distance(indices(n)), + // which will be a wider type, until we get to an unsupported type. + // This verifies that indices(n) is SFINAE-friendly, because otherwise we + // would get a hard error outside the immediate context checked by requires. + if constexpr (requires { std::views::indices(n); }) + return test_wider(std::ranges::distance(std::views::indices(n))); + return sizeof(T); +} + +static_assert(test_wider(0) > sizeof(long long));
