[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;
+}
----------------
frederick-vs-ja wrote:

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

Ditto below, I think we should only use `static_assert` in this test file.

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


More information about the libcxx-commits mailing list