From: Jonathan Wakely Date: Tue, 21 Oct 2025 20:21:45 +0000 (+0100) Subject: libstdc++: Add missing constraints to views::indices X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=437ed2fec78cbd72cd31922b7d506847e45c52f9;p=thirdparty%2Fgcc.git 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 header with needed for size_t. --- diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 25d2e28e72f..158692d92a7 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -791,6 +791,7 @@ namespace views struct _Indices { template + 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 805b29e358f..038b38f0ce0 100644 --- a/libstdc++-v3/testsuite/std/ranges/indices/1.cc +++ b/libstdc++-v3/testsuite/std/ranges/indices/1.cc @@ -4,7 +4,7 @@ #include #include -#include +#include template constexpr bool test(T n) { @@ -29,3 +29,17 @@ int main() { VERIFY(test(44)); static_assert(test(44)); } + +template +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));