[libcxx-commits] [libcxx] [libc++] Implement a type-safe iterator for optional (PR #154239)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jan 14 14:07:26 PST 2026
================
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <optional>
+
+// Add to iterator out of bounds.
+
+// REQUIRES: std-at-least-c++26
+// UNSUPPORTED: libcpp-hardening-mode=none, libcpp-has-abi-bounded-iterators-in-optional
+
+#include <optional>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ std::optional<int> opt(1);
+ auto i = opt.begin();
+
+ TEST_LIBCPP_ASSERT_FAILURE(
+ i += 2,
+ "__capacity_aware_iterator::operator+=: Attempting to move iterator past its container's possible range");
+
+ TEST_LIBCPP_ASSERT_FAILURE(
+ i += -2,
+ "__capacity_aware_iterator::operator+=: Attempting to move iterator past its container's possible range");
+
+ TEST_LIBCPP_ASSERT_FAILURE(
+ i -= 2,
+ "__capacity_aware_iterator::operator-=: Attempting to move iterator past its container's possible range");
+
+ TEST_LIBCPP_ASSERT_FAILURE(
+ i -= -2,
+ "__capacity_aware_iterator::operator-=: Attempting to move iterator past its container's possible range");
+
+ TEST_LIBCPP_ASSERT_FAILURE(
+ i[2],
+ "__capacity_aware_iterator::operator[]: Attempting to index iterator past its container's possible range");
+
+ TEST_LIBCPP_ASSERT_FAILURE(
+ i[-2],
+ "__capacity_aware_iterator::operator[]: Attempting to index iterator past its container's possible range");
+ }
+}
----------------
ldionne wrote:
`return 0;`
https://github.com/llvm/llvm-project/pull/154239
More information about the libcxx-commits
mailing list