From 1fa5dd8f9862c3314b4499c944906424d7a5a8e7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomasz=20Kami=C5=84ski?= Date: Thu, 6 Nov 2025 16:02:42 +0100 Subject: [PATCH] libstdc++: Add ranges::borrowed_range specialization for optional [PR122425] MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- libstdc++-v3/include/std/optional | 6 ++++++ libstdc++-v3/testsuite/20_util/optional/range.cc | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) 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 -- 2.47.3