]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add ranges::borrowed_range specialization for optional<T&> [PR122425]
authorTomasz Kamiński <tkaminsk@redhat.com>
Thu, 6 Nov 2025 15:02:42 +0000 (16:02 +0100)
committerTomasz Kamiński <tkaminsk@redhat.com>
Wed, 12 Nov 2025 08:25:54 +0000 (09:25 +0100)
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 <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
libstdc++-v3/include/std/optional
libstdc++-v3/testsuite/20_util/optional/range.cc

index d191e51ed79ab95b83fca41cc21335f0ac497f4e..75a9531ccd5dd33728eb16d9d7a04dff2ea97e7c 100644 (file)
@@ -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;
index 1cb3eb6ceff01c7d043bff198191656fc4500129..981969cb6142c4c9d3528ab4ecb8acdeddedcb8a 100644 (file)
@@ -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>