From: Jonathan Wakely Date: Thu, 14 Nov 2024 17:31:43 +0000 (+0000) Subject: libstdc++: Fix get<0> constraint for lvalue ranges::subrange (LWG 3589) X-Git-Tag: releases/gcc-12.5.0~216 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d8ee74e424c9d42b8164296dda0ec604a971253;p=thirdparty%2Fgcc.git libstdc++: Fix get<0> constraint for lvalue ranges::subrange (LWG 3589) Approved at October 2021 plenary. libstdc++-v3/ChangeLog: * include/bits/ranges_util.h (subrange::begin): Fix constraint, as per LWG 3589. * testsuite/std/ranges/subrange/lwg3589.cc: New test. (cherry picked from commit 4a3a0be34f723df192361e43bb48b9292dfe3a54) --- diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h index 37d7bc862f9f..5299ce193c93 100644 --- a/libstdc++-v3/include/bits/ranges_util.h +++ b/libstdc++-v3/include/bits/ranges_util.h @@ -399,8 +399,11 @@ namespace ranges __detail::__make_unsigned_like_t>) -> subrange, sentinel_t<_Rng>, subrange_kind::sized>; + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3589. The const lvalue reference overload of get for subrange does not + // constrain I to be copyable when N == 0 template - requires (_Num < 2) + requires ((_Num == 0 && copyable<_It>) || _Num == 1) constexpr auto get(const subrange<_It, _Sent, _Kind>& __r) { diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/lwg3589.cc b/libstdc++-v3/testsuite/std/ranges/subrange/lwg3589.cc new file mode 100644 index 000000000000..1ccc52d81f8a --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/subrange/lwg3589.cc @@ -0,0 +1,30 @@ +// { dg-do compile { target c++20 } } + +// LWG 3589. The const lvalue reference overload of get for subrange does not +// constrain I to be copyable when N == 0 + +#include +#include + +void +test_lwg3589() +{ + int a[2]{}; + __gnu_test::test_range r(a); + + // Use a generic lambda so we have a dependent context. + auto test = [](auto& x) + { + // This was wrong before the LWG 3589 change: + if constexpr (requires { std::ranges::get<0>(x); }) + (void) std::ranges::get<0>(x); + + // These always worked unconditionally: + (void) std::ranges::get<1>(x); + (void) std::ranges::get<0>(std::move(x)); + (void) std::ranges::get<1>(std::move(x)); + }; + + std::ranges::subrange sr(r.begin(), r.end()); + test(sr); +}