https://gcc.gnu.org/g:1fa5dd8f9862c3314b4499c944906424d7a5a8e7
commit r16-5187-g1fa5dd8f9862c3314b4499c944906424d7a5a8e7 Author: Tomasz Kamiński <[email protected]> Date: Thu Nov 6 16:02:42 2025 +0100 libstdc++: Add ranges::borrowed_range specialization for optional<T&> [PR122425] PR libstdc++/122425 libstdc++-v3/ChangeLog: * include/std/optional (ranges::enable_borrowed_range<optional<_Tp&>>): Define. * testsuite/20_util/optional/range.cc: Update tests. Reviewed-by: Jonathan Wakely <[email protected]> Signed-off-by: Tomasz Kamiński <[email protected]> Diff: --- 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 d191e51ed79a..75a9531ccd5d 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<optional<_Tp>> = true; +#if __cpp_lib_optional >= 202506L // C++26 + template<typename _Tp> + constexpr bool + ranges::enable_borrowed_range<optional<_Tp&>> = true; +#endif + template<typename _Tp> inline constexpr range_format format_kind<optional<_Tp>> = range_format::disabled; diff --git a/libstdc++-v3/testsuite/20_util/optional/range.cc b/libstdc++-v3/testsuite/20_util/optional/range.cc index 1cb3eb6ceff0..981969cb6142 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<O>); static_assert(std::ranges::sized_range<O>); static_assert(std::ranges::common_range<O>); - static_assert(!std::ranges::borrowed_range<O>); + + // an optional<T&> is borrowed range + constexpr bool is_ref_opt = std::is_reference_v<T>; + static_assert(std::ranges::borrowed_range<O> == is_ref_opt); // an optional<const T> is not assignable, and therefore does not satisfy ranges::view constexpr bool is_const_opt = std::is_const_v<T>; static_assert(std::ranges::view<O> == !is_const_opt); static_assert(std::ranges::viewable_range<O> == !is_const_opt); + } template<typename O>
