[libcxx-commits] [libcxx] [libc++] Implement P3168R2: Give optional range support (PR #149441)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 1 09:28:22 PDT 2025


================
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// constexpr iterator optional::begin() noexcept;
+// constexpr const_iterator optional::begin() noexcept;
+
+#include <cassert>
+#include <iterator>
+#include <optional>
+#include <type_traits>
+#include <utility>
+
+template <typename T>
+constexpr bool test() {
+  const std::optional<T> opt{T{}};
+  std::optional<T> nonconst_opt{T{}};
+
+  { // begin() is marked noexcept
+    static_assert(noexcept(opt.begin()));
+    static_assert(noexcept(nonconst_opt.begin()));
+  }
+
+  { // Dereferencing an iterator at the beginning == indexing the 0th element, and that calling begin() again return the same iterator.
+    auto iter1 = opt.begin();
+    auto iter2 = nonconst_opt.begin();
----------------
ldionne wrote:

Both of these optionals are non-const. I believe that using `std::as_const` consistently throughout your tests would resolve the possibility for these kinds of mixups!

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


More information about the libcxx-commits mailing list