[libcxx-commits] [libcxx] [libc++] Implement a type-safe iterator for optional (PR #154239)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 5 08:57:52 PST 2025


================
@@ -86,6 +86,30 @@ constexpr bool test() {
     assert(*(val.begin()) == v);
   }
 
+  // [container.reqmts] operator-
+  {
+    std::optional<T> val(v);
+    auto it1 = val.begin();
+    auto it2 = val.begin();
+    auto it3 = val.end();
+
+    auto cit1 = std::as_const(val).begin();
+    auto cit2 = std::as_const(val).begin();
+    auto cit3 = std::as_const(val).end();
+
+    assert(it1 - it2 == 0);
+    assert(cit1 - cit2 == 0);
+    assert(it1 - cit1 == 0);
+    assert(it3 - it1 == 1);
+    assert(it1 - it3 == -1);
+
+    assert(cit3 - cit1 == 1);
+    assert(cit1 - cit3 == -1);
+    assert(cit3 - cit3 == 0);
+    assert(cit3 - it1 == 1);
+    assert(it1 - cit3 == -1);
+  }
+
   return true;
 }
 
----------------
ldionne wrote:

Below, as a drive by, I'd remove parens in `assert((test<int, 1>()))`. Pretty sure they're not required.

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


More information about the libcxx-commits mailing list