[libcxx-commits] [libcxx] [libc++] Implement P2988R12: `std::optional<T&>` (PR #155202)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Nov 9 02:29:25 PST 2025


================
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <optional>
+
+// template <class T> class optional<T&>::iterator;
+// template <class T> class optional<T&>::const_iterator;
+// template <class T>
+// constexpr bool ranges::enable_borrowed_range<optional<T&>> = true;
+
+#include <cassert>
+#include <optional>
+#include <ranges>
+
+template <typename T>
+constexpr bool enable_borrowed_range() {
+  {
+    assert(std::ranges::enable_borrowed_range<std::optional<T&>>);
+  }
+  return true;
+}
+
+template <typename T>
+constexpr bool borrowed_range() {
+  if (std::ranges::range<std::optional<T&>>) {
+    assert(std::ranges::borrowed_range<std::optional<T&>>);
+  } else {
+    assert(!std::ranges::borrowed_range<std::optional<T&>>);
+    return false;
+  }
+
+  return true;
+}
----------------
frederick-vs-ja wrote:

```suggestion
template <typename T>
void borrowed_range() {
  static_assert(std::ranges::borrowed_range<std::optional<T&>> == std::ranges::range<std::optional<T&>>);
}
```

So that we won't need to touch this file again when implementing LWG4308, as we've been testing range-ness in `iterator.pass.cpp`.

https://github.com/llvm/llvm-project/pull/155202


More information about the libcxx-commits mailing list