From: Tomasz Kamiński Date: Thu, 6 Nov 2025 15:02:42 +0000 (+0100) Subject: libstdc++: Add ranges::borrowed_range specialization for optional [PR122425] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1fa5dd8f9862c3314b4499c944906424d7a5a8e7;p=thirdparty%2Fgcc.git libstdc++: Add ranges::borrowed_range specialization for optional [PR122425] PR libstdc++/122425 libstdc++-v3/ChangeLog: * include/std/optional (ranges::enable_borrowed_range>): Define. * testsuite/20_util/optional/range.cc: Update tests. Reviewed-by: Jonathan Wakely Signed-off-by: Tomasz Kamiński --- diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index d191e51ed79..75a9531ccd5 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -2182,6 +2182,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline constexpr bool ranges::enable_view> = true; +#if __cpp_lib_optional >= 202506L // C++26 + template + constexpr bool + ranges::enable_borrowed_range> = true; +#endif + template inline constexpr range_format format_kind> = range_format::disabled; diff --git a/libstdc++-v3/testsuite/20_util/optional/range.cc b/libstdc++-v3/testsuite/20_util/optional/range.cc index 1cb3eb6ceff..981969cb614 100644 --- a/libstdc++-v3/testsuite/20_util/optional/range.cc +++ b/libstdc++-v3/testsuite/20_util/optional/range.cc @@ -19,12 +19,16 @@ test_range_concepts() static_assert(std::ranges::contiguous_range); static_assert(std::ranges::sized_range); static_assert(std::ranges::common_range); - static_assert(!std::ranges::borrowed_range); + + // an optional is borrowed range + constexpr bool is_ref_opt = std::is_reference_v; + static_assert(std::ranges::borrowed_range == is_ref_opt); // an optional is not assignable, and therefore does not satisfy ranges::view constexpr bool is_const_opt = std::is_const_v; static_assert(std::ranges::view == !is_const_opt); static_assert(std::ranges::viewable_range == !is_const_opt); + } template