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

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 25 01:44:02 PDT 2025


================
@@ -257,8 +257,88 @@ constexpr bool test() {
   return true;
 }
 
+#if TEST_STD_VER >= 26
+constexpr bool test_ref() {
+  // Test & overload
+  {
+    // Without & qualifier on F's operator()
+    {
+      int j = 42;
+      std::optional<int&> i{j};
+      assert(i.and_then(LVal{}) == 1);
+      assert(i.and_then(NOLVal{}) == std::nullopt);
+      ASSERT_SAME_TYPE(decltype(i.and_then(LVal{})), std::optional<int>);
----------------
H-G-Hristov wrote:

```suggestion
      int j = 42;
      std::optional<int&> i{j};
      
      std::same_as<std::optional<int> decltype(auto) r = i.and_then(LVal{});
      assert(r == 1);
      assert(i.and_then(NOLVal{}) == std::nullopt);
```
Sugestion as a preferred alternative AFAIK to `ASSERT_SAME_TYPE`: `std::same_as<std::optional<int> decltype(auto) ....` with C++20...

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


More information about the libcxx-commits mailing list