[libcxx-commits] [libcxx] [libc++][NFC] Simplify some `optional.observe` tests (PR #175682)
William Tran-Viet via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jan 13 16:55:00 PST 2026
https://github.com/smallp-o-p updated https://github.com/llvm/llvm-project/pull/175682
>From 760507d24cb558e2dc2ea6c0820d0b72ec748bd3 Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Fri, 9 Jan 2026 15:40:39 -0500
Subject: [PATCH 1/5] Consolidate operator*
---
.../dereference.pass.cpp | 130 +++++++++++-------
.../dereference_const.pass.cpp | 93 -------------
.../dereference_const_rvalue.pass.cpp | 93 -------------
.../dereference_rvalue.pass.cpp | 97 -------------
4 files changed, 84 insertions(+), 329 deletions(-)
delete mode 100644 libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const.pass.cpp
delete mode 100644 libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const_rvalue.pass.cpp
delete mode 100644 libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_rvalue.pass.cpp
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
index 72cf0446c7eb9..acd900a3d6638 100644
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
@@ -10,6 +10,10 @@
// <optional>
// constexpr T& optional<T>::operator*() &;
+// constexpr const T& optional<T>::operator*() const&;
+// constexpr T&& optional<T>::operator*() &&;
+// constexpr const T&& optional<T>::operator*() const&&;
+// constexpr T& optional<T&>::operator*() const;
#include <cassert>
#include <memory>
@@ -22,35 +26,52 @@
using std::optional;
-struct X
-{
- constexpr int test() const& {return 3;}
- int test() & {return 4;}
- constexpr int test() const&& {return 5;}
- int test() && {return 6;}
+struct X {
+ constexpr int test() const& { return 3; }
+ constexpr int test() & { return 4; }
+ constexpr int test() const&& { return 5; }
+ constexpr int test() && { return 6; }
};
-struct Y
-{
- constexpr int test() {return 7;}
-};
+template <typename T>
+constexpr void test_contract() {
+ std::optional<T> opt(T{});
+
+ ASSERT_SAME_TYPE(decltype(*opt), T&);
+ ASSERT_SAME_TYPE(decltype(*std::move(opt)), T&&);
+ ASSERT_SAME_TYPE(decltype(*std::as_const(opt)), const T&);
+ ASSERT_SAME_TYPE(decltype(*std::move(std::as_const(opt))), const T&&);
-constexpr int
-test()
-{
- optional<Y> opt{Y{}};
- return (*opt).test();
+ ASSERT_NOEXCEPT(*opt);
+ ASSERT_NOEXCEPT(*std::move(opt));
+ ASSERT_NOEXCEPT(*std::as_const(opt));
+ ASSERT_NOEXCEPT(*std::move(std::as_const(opt)));
}
#if TEST_STD_VER >= 26
-constexpr bool test_ref() {
+template <typename T>
+constexpr void test_ref_contract() {
+ std::optional<T&> opt;
+ ASSERT_SAME_TYPE(decltype(*opt), T&);
+ ASSERT_SAME_TYPE(decltype(*std::move(opt)), T&);
+ ASSERT_SAME_TYPE(decltype(*std::as_const(opt)), T&);
+ ASSERT_SAME_TYPE(decltype(*std::move(std::as_const(opt))), T&);
+
+ ASSERT_NOEXCEPT(*opt);
+ ASSERT_NOEXCEPT(*std::move(opt));
+ ASSERT_NOEXCEPT(*std::as_const(opt));
+ ASSERT_NOEXCEPT(*std::move(std::as_const(opt)));
+}
+
+constexpr void test_ref() {
{
TracedCopyMove x{};
std::optional<TracedCopyMove&> opt(x);
- static_assert(noexcept(*opt));
- ASSERT_SAME_TYPE(decltype(*opt), TracedCopyMove&);
assert(std::addressof(*opt) == std::addressof(x));
+ assert(std::addressof(*std::move(opt)) == std::addressof(x));
+ assert(std::addressof(*std::as_const(opt)) == std::addressof(x));
+ assert(std::addressof(*std::move(std::as_const(opt))) == std::addressof(x));
assert(x.constMove == 0);
assert(x.nonConstMove == 0);
assert(x.constCopy == 0);
@@ -60,44 +81,61 @@ constexpr bool test_ref() {
{
TracedCopyMove x{};
std::optional<const TracedCopyMove&> opt(x);
- static_assert(noexcept(*opt));
- ASSERT_SAME_TYPE(decltype(*opt), const TracedCopyMove&);
assert(std::addressof(*opt) == std::addressof(x));
+ assert(std::addressof(*std::move(opt)) == std::addressof(x));
+ assert(std::addressof(*std::as_const(opt)) == std::addressof(x));
+ assert(std::addressof(*std::move(std::as_const(opt))) == std::addressof(x));
assert(x.constMove == 0);
assert(x.nonConstMove == 0);
assert(x.constCopy == 0);
assert(x.nonConstCopy == 0);
}
-
- return true;
+ {
+ X x{};
+ optional<X&> opt(x);
+ assert((*opt).test() == 4);
+ }
}
#endif
-int main(int, char**)
-{
- {
- optional<X> opt; ((void)opt);
- ASSERT_SAME_TYPE(decltype(*opt), X&);
- ASSERT_NOEXCEPT(*opt);
- }
- {
- optional<X> opt(X{});
- assert((*opt).test() == 4);
- }
+constexpr bool test() {
+ test_contract<int>();
+ test_contract<float>();
+ test_contract<double>();
+ test_contract<X>();
+ test_contract<const int>();
+ test_contract<const float>();
+ test_contract<const double>();
+ test_contract<const X>();
+
+ std::optional<X> opt{X{}};
+ {
+ assert((*std::as_const(opt)).test() == 3);
+ assert((*opt).test() == 4);
+ assert((*std::move(std::as_const(opt))).test() == 5);
+ assert((*std::move(opt)).test() == 6);
+ }
+
#if TEST_STD_VER >= 26
- {
- X x{};
- optional<X&> opt(x);
- ASSERT_SAME_TYPE(decltype(*opt), X&);
- ASSERT_NOEXCEPT(*opt);
- }
- {
- X x{};
- optional<X&> opt(x);
- assert((*opt).test() == 4);
- }
+ test_ref_contract<int>();
+ test_ref_contract<float>();
+ test_ref_contract<double>();
+ test_ref_contract<X>();
+ test_ref_contract<const int>();
+ test_ref_contract<const float>();
+ test_ref_contract<const double>();
+ test_ref_contract<const X>();
+
+ test_ref();
#endif
- static_assert(test() == 7, "");
- return 0;
+
+ return true;
+}
+
+int main(int, char**) {
+ assert(test());
+ static_assert(test());
+
+ return 0;
}
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const.pass.cpp
deleted file mode 100644
index aefe8d37a142a..0000000000000
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const.pass.cpp
+++ /dev/null
@@ -1,93 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <optional>
-
-// constexpr const T& optional<T>::operator*() const &;
-
-#include <cassert>
-#include <memory>
-#include <optional>
-
-#include "test_macros.h"
-#if TEST_STD_VER >= 26
-# include "copy_move_types.h"
-#endif
-
-using std::optional;
-
-struct X
-{
- constexpr int test() const& {return 3;}
- int test() & {return 4;}
- constexpr int test() const&& {return 5;}
- int test() && {return 6;}
-};
-
-struct Y
-{
- int test() const {return 2;}
-};
-
-#if TEST_STD_VER >= 26
-constexpr bool test_ref() {
- using T = TracedCopyMove;
- {
- T x{};
- const std::optional<T&> opt(x);
- ASSERT_NOEXCEPT(*opt);
- ASSERT_SAME_TYPE(decltype(*opt), TracedCopyMove&);
-
- assert(std::addressof(*opt) == std::addressof(x));
- assert((*opt).constMove == 0);
- assert((*opt).nonConstMove == 0);
- assert((*opt).constCopy == 0);
- assert((*opt).nonConstCopy == 0);
- }
-
- {
- T x{};
- const std::optional<const T&> opt(x);
- ASSERT_NOEXCEPT(*opt);
- ASSERT_SAME_TYPE(decltype(*opt), const TracedCopyMove&);
-
- assert(std::addressof(*opt) == std::addressof(x));
- assert((*opt).constMove == 0);
- assert((*opt).nonConstMove == 0);
- assert((*opt).constCopy == 0);
- assert((*opt).nonConstCopy == 0);
- }
-
- return true;
-}
-#endif
-
-int main(int, char**)
-{
- {
- const optional<X> opt; ((void)opt);
- ASSERT_SAME_TYPE(decltype(*opt), X const&);
- ASSERT_NOEXCEPT(*opt);
- }
- {
- constexpr optional<X> opt(X{});
- static_assert((*opt).test() == 3, "");
- }
- {
- constexpr optional<Y> opt(Y{});
- assert((*opt).test() == 2);
- }
-#if TEST_STD_VER >= 26
- {
- assert(test_ref());
- static_assert(test_ref());
- }
-#endif
- return 0;
-}
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const_rvalue.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const_rvalue.pass.cpp
deleted file mode 100644
index cbff9cf3b1d6b..0000000000000
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const_rvalue.pass.cpp
+++ /dev/null
@@ -1,93 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <optional>
-
-// constexpr T&& optional<T>::operator*() const &&;
-
-#include <cassert>
-#include <memory>
-#include <optional>
-#include <utility>
-
-#include "test_macros.h"
-#if TEST_STD_VER >= 26
-# include "copy_move_types.h"
-#endif
-
-using std::optional;
-
-struct X
-{
- constexpr int test() const& {return 3;}
- int test() & {return 4;}
- constexpr int test() const&& {return 5;}
- int test() && {return 6;}
-};
-
-struct Y
-{
- int test() const && {return 2;}
-};
-
-#if TEST_STD_VER >= 26
-constexpr bool test_ref() {
- { // const&&
- TracedCopyMove x{};
- const std::optional<TracedCopyMove&> opt(x);
- ASSERT_NOEXCEPT(*std::move(opt));
- ASSERT_SAME_TYPE(decltype(*std::move(opt)), TracedCopyMove&);
-
- assert(std::addressof(*(std::move(opt))) == std::addressof(x));
- assert((*std::move(opt)).constMove == 0);
- assert((*std::move(opt)).nonConstMove == 0);
- assert((*std::move(opt)).constCopy == 0);
- assert((*std::move(opt)).nonConstCopy == 0);
- }
-
- {
- TracedCopyMove x{};
- const std::optional<const TracedCopyMove&> opt(x);
- ASSERT_NOEXCEPT(*std::move(opt));
- ASSERT_SAME_TYPE(decltype(*std::move(opt)), const TracedCopyMove&);
-
- assert(std::addressof(*(std::move(opt))) == std::addressof(x));
- assert((*std::move(opt)).constMove == 0);
- assert((*std::move(opt)).nonConstMove == 0);
- assert((*std::move(opt)).constCopy == 0);
- assert((*std::move(opt)).nonConstCopy == 0);
- }
-
- return true;
-}
-#endif
-
-int main(int, char**)
-{
- {
- const optional<X> opt; ((void)opt);
- ASSERT_SAME_TYPE(decltype(*std::move(opt)), X const &&);
- ASSERT_NOEXCEPT(*std::move(opt));
- }
- {
- constexpr optional<X> opt(X{});
- static_assert((*std::move(opt)).test() == 5, "");
- }
- {
- constexpr optional<Y> opt(Y{});
- assert((*std::move(opt)).test() == 2);
- }
-
-#if TEST_STD_VER >= 26
- assert(test_ref());
- static_assert(test_ref());
-#endif
-
- return 0;
-}
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_rvalue.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_rvalue.pass.cpp
deleted file mode 100644
index 9c394bb13a2a0..0000000000000
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_rvalue.pass.cpp
+++ /dev/null
@@ -1,97 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <optional>
-
-// constexpr T&& optional<T>::operator*() &&;
-
-#include <cassert>
-#include <optional>
-
-#include "test_macros.h"
-#if TEST_STD_VER >= 26
-# include "copy_move_types.h"
-#endif
-
-using std::optional;
-
-struct X
-{
- constexpr int test() const& {return 3;}
- int test() & {return 4;}
- constexpr int test() const&& {return 5;}
- int test() && {return 6;}
-};
-
-struct Y
-{
- constexpr int test() && {return 7;}
-};
-
-constexpr int
-test()
-{
- optional<Y> opt{Y{}};
- return (*std::move(opt)).test();
-}
-
-#if TEST_STD_VER >= 26
-constexpr bool test_ref() {
- // ensure underlying value isn't moved from
- {
- TracedCopyMove x{};
- std::optional<TracedCopyMove&> opt(x);
- ASSERT_NOEXCEPT(*std::move(opt));
- ASSERT_SAME_TYPE(decltype(*std::move(opt)), TracedCopyMove&);
-
- assert(std::addressof(*std::move(opt)) == std::addressof(x));
- assert((*std::move(opt)).constMove == 0);
- assert((*std::move(opt)).nonConstMove == 0);
- assert((*std::move(opt)).constCopy == 0);
- assert((*std::move(opt)).nonConstCopy == 0);
- }
-
- {
- TracedCopyMove x{};
- std::optional<const TracedCopyMove&> opt(x);
- ASSERT_NOEXCEPT(*std::move(opt));
- ASSERT_SAME_TYPE(decltype(*std::move(opt)), const TracedCopyMove&);
-
- assert(std::addressof(*std::move(opt)) == std::addressof(x));
- assert((*std::move(opt)).constMove == 0);
- assert((*std::move(opt)).nonConstMove == 0);
- assert((*std::move(opt)).constCopy == 0);
- assert((*std::move(opt)).nonConstCopy == 0);
- }
-
- return true;
-}
-
-#endif
-
-int main(int, char**)
-{
- {
- optional<X> opt; ((void)opt);
- ASSERT_SAME_TYPE(decltype(*std::move(opt)), X&&);
- ASSERT_NOEXCEPT(*std::move(opt));
- }
- {
- optional<X> opt(X{});
- assert((*std::move(opt)).test() == 6);
- }
- static_assert(test() == 7, "");
-
-#if TEST_STD_VER >= 26
- assert(test_ref());
- static_assert(test_ref());
-#endif
-
- return 0;
-}
>From 308b849573ff7760b82fbace9500862e1bc2d271 Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Fri, 9 Jan 2026 15:53:13 -0500
Subject: [PATCH 2/5] Consolidate operator->
---
.../optional.object.observe/op_arrow.pass.cpp | 151 ++++++++++++------
.../op_arrow_const.pass.cpp | 81 ----------
2 files changed, 99 insertions(+), 133 deletions(-)
delete mode 100644 libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow_const.pass.cpp
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp
index bb050f20debe1..a3e58183a2d60 100644
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp
@@ -9,74 +9,121 @@
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
-// constexpr T* optional<T>::operator->();
+// constexpr T* optional<T>::operator->() noexcept;
+// constexpr const T* optional<T>::operator->() const noexcept;
+// constexpr T* optional<T&>::operator->() const noexcept;
#include <cassert>
#include <memory>
#include <optional>
+#include <utility>
#include "test_macros.h"
using std::optional;
struct X {
- int test() noexcept { return 3; }
- int test() const noexcept { return 3; }
+ constexpr int test() noexcept { return 3; }
+ constexpr int test() const noexcept { return 4; }
};
-struct Y
-{
- constexpr int test() {return 3;}
-};
+template <typename T>
+constexpr void test_contract() {
+ std::optional<T> opt;
+ ASSERT_SAME_TYPE(decltype(opt.operator->()), T*);
+ ASSERT_SAME_TYPE(decltype(std::as_const(opt).operator->()), const T*);
-constexpr int
-test()
-{
- optional<Y> opt{Y{}};
- return opt->test();
+ ASSERT_NOEXCEPT(opt.operator->());
+ ASSERT_NOEXCEPT(std::as_const(opt).operator->());
}
-int main(int, char**)
-{
- {
- std::optional<X> opt; ((void)opt);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
- ASSERT_NOEXCEPT(opt.operator->());
- }
- {
- optional<X> opt(X{});
- assert(opt->test() == 3);
- }
#if TEST_STD_VER >= 26
- {
- X x{};
- std::optional<X&> opt(x);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
- ASSERT_NOEXCEPT(opt.operator->());
- assert(opt.operator->() == std::addressof(x));
- }
- {
- X x{};
- std::optional<const X&> opt(x);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), const X*);
- ASSERT_NOEXCEPT(opt.operator->());
- assert(opt.operator->() == std::addressof(x));
- }
- {
- X x{};
- optional<X&> opt{x};
- assert(opt->test() == 3);
- assert(opt.operator->() == std::addressof(x));
- }
- {
- X x{};
- optional<const X&> opt{x};
- assert(opt->test() == 3);
- }
+template <typename T>
+constexpr void test_ref_contract() {
+ std::optional<T&> opt;
+
+ ASSERT_SAME_TYPE(decltype(opt.operator->()), T*);
+ ASSERT_SAME_TYPE(decltype(std::as_const(opt).operator->()), T*);
+
+ ASSERT_NOEXCEPT(opt.operator->());
+ ASSERT_NOEXCEPT(std::as_const(opt).operator->());
+}
+
+constexpr void test_ref() {
+ {
+ X x{};
+ std::optional<X&> opt(x);
+ ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
+ ASSERT_NOEXCEPT(opt.operator->());
+ assert(opt.operator->() == std::addressof(x));
+ assert(opt->test() == 3);
+ }
+ {
+ X x{};
+ std::optional<const X&> opt(x);
+ ASSERT_SAME_TYPE(decltype(opt.operator->()), const X*);
+ ASSERT_NOEXCEPT(opt.operator->());
+ assert(opt.operator->() == std::addressof(x));
+ assert(opt->test() == 4);
+ }
+ {
+ X x{};
+ const std::optional<X&> opt(x);
+ ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
+ ASSERT_NOEXCEPT(opt.operator->());
+ assert(opt.operator->() == std::addressof(x));
+ assert(opt->test() == 3);
+ }
+ {
+ X x{};
+ const std::optional<const X&> opt(x);
+ ASSERT_SAME_TYPE(decltype(opt.operator->()), const X*);
+ ASSERT_NOEXCEPT(opt.operator->());
+ assert(opt.operator->() == std::addressof(x));
+ assert(opt->test() == 4);
+ }
+}
+
+#endif
+
+constexpr bool test() {
+ test_contract<int>();
+ test_contract<float>();
+ test_contract<double>();
+ test_contract<X>();
+
+ test_contract<const int>();
+ test_contract<const float>();
+ test_contract<const double>();
+ test_contract<const X>();
+
+ std::optional<X> opt{X{}};
+ {
+ assert(opt->test() == 3);
+ assert(std::as_const(opt)->test() == 4);
+ assert(opt.operator->() == &*opt);
+ assert(std::as_const(opt).operator->() == &*opt);
+ }
+
+#if TEST_STD_VER >= 26
+ test_ref_contract<int>();
+ test_ref_contract<float>();
+ test_ref_contract<double>();
+ test_ref_contract<X>();
+
+ test_ref_contract<const int>();
+ test_ref_contract<const float>();
+ test_ref_contract<const double>();
+
+ test_ref();
#endif
- {
- static_assert(test() == 3, "");
- }
- return 0;
+ return true;
+}
+
+int main(int, char**) {
+ assert(test());
+ static_assert(test());
+
+ return 0;
}
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow_const.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow_const.pass.cpp
deleted file mode 100644
index 0461a04b85d45..0000000000000
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow_const.pass.cpp
+++ /dev/null
@@ -1,81 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <optional>
-
-// constexpr const T* optional<T>::operator->() const;
-
-#include <cassert>
-#include <memory>
-#include <optional>
-
-#include "test_macros.h"
-
-using std::optional;
-
-struct X
-{
- constexpr int test() const {return 3;}
-};
-
-struct Y
-{
- int test() const noexcept {return 2;}
-};
-
-struct Z
-{
- const Z* operator&() const;
- constexpr int test() const {return 1;}
-};
-
-int main(int, char**)
-{
- {
- const std::optional<X> opt; ((void)opt);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), X const*);
- ASSERT_NOEXCEPT(opt.operator->());
- }
- {
- constexpr optional<X> opt(X{});
- static_assert(opt->test() == 3, "");
- }
- {
- constexpr optional<Y> opt(Y{});
- assert(opt->test() == 2);
- }
- {
- constexpr optional<Z> opt(Z{});
- static_assert(opt->test() == 1, "");
- }
-#if TEST_STD_VER >= 26
- {
- X x{};
- const std::optional<X&> opt(x);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
- ASSERT_NOEXCEPT(opt.operator->());
- assert(opt.operator->() == std::addressof(x));
- }
- {
- X x{};
- const std::optional<const X&> opt(x);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), const X*);
- ASSERT_NOEXCEPT(opt.operator->());
- assert(opt.operator->() == std::addressof(x));
- }
- {
- static constexpr Z z{};
- constexpr optional<const Z&> opt(z);
- static_assert(opt->test() == 1);
- static_assert(opt.operator->() == std::addressof(z));
- }
-#endif
-
- return 0;
-}
>From 008b26f2ef3abecbb5bfd1e0a18674d71d1b5601 Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Fri, 9 Jan 2026 16:26:01 -0500
Subject: [PATCH 3/5] Consolidate value()
---
.../optional.object.observe/value.pass.cpp | 199 +++++++++++++-----
.../value_const.compile.fail.cpp | 34 ---
.../value_const.pass.cpp | 106 ----------
.../value_const_rvalue.pass.cpp | 106 ----------
.../value_rvalue.pass.cpp | 73 -------
5 files changed, 150 insertions(+), 368 deletions(-)
delete mode 100644 libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.compile.fail.cpp
delete mode 100644 libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.pass.cpp
delete mode 100644 libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const_rvalue.pass.cpp
delete mode 100644 libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_rvalue.pass.cpp
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp
index 22b74f5512d53..0bea13cfc5f82 100644
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp
@@ -11,73 +11,174 @@
// <optional>
// constexpr T& optional<T>::value() &;
+// constexpr const T& optional<T>::value() const&;
+// constexpr T&& optional<T>::value() &&;
+// constexpr const T&& optional<T>::value() const T&&;
+// constexpr T& optional<T&>::value() const;
-#include <optional>
-#include <type_traits>
#include <cassert>
+#include <optional>
+#include <utility>
#include "test_macros.h"
-using std::optional;
using std::bad_optional_access;
+using std::optional;
-struct X
-{
- X() = default;
- X(const X&) = delete;
- constexpr int test() const & {return 3;}
- int test() & {return 4;}
- constexpr int test() const && {return 5;}
- int test() && {return 6;}
+struct X {
+ constexpr X() = default;
+ constexpr int test() const& { return 3; }
+ constexpr int test() & { return 4; }
+ constexpr int test() const&& { return 5; }
+ constexpr int test() && { return 6; }
};
-struct Y
-{
- constexpr int test() & {return 7;}
-};
+template <typename T>
+constexpr void test_contract() {
+ std::optional<T> opt;
-constexpr int
-test()
-{
- optional<Y> opt{Y{}};
- return opt.value().test();
+ ASSERT_SAME_TYPE(decltype(opt.value()), T&);
+ ASSERT_SAME_TYPE(decltype(std::as_const(opt).value()), const T&);
+ ASSERT_SAME_TYPE(decltype(std::move(opt).value()), T&&);
+ ASSERT_SAME_TYPE(decltype(std::move(std::as_const(opt)).value()), const T&&);
+
+ ASSERT_NOT_NOEXCEPT(opt.value());
+ ASSERT_NOT_NOEXCEPT(std::as_const(opt).value());
+ ASSERT_NOT_NOEXCEPT(std::move(opt).value());
+ ASSERT_NOT_NOEXCEPT(std::move(std::as_const(opt)).value());
}
+#if TEST_STD_VER >= 26
+template <typename T>
+constexpr void test_ref_contract() {
+ std::optional<T&> opt;
+
+ ASSERT_SAME_TYPE(decltype(opt.value()), T&);
+ ASSERT_SAME_TYPE(decltype(std::as_const(opt).value()), T&);
+ ASSERT_SAME_TYPE(decltype(std::move(opt).value()), T&);
+ ASSERT_SAME_TYPE(decltype(std::move(std::as_const(opt)).value()), T&);
+
+ ASSERT_NOT_NOEXCEPT(opt.value());
+ ASSERT_NOT_NOEXCEPT(std::as_const(opt).value());
+ ASSERT_NOT_NOEXCEPT(std::move(opt).value());
+ ASSERT_NOT_NOEXCEPT(std::move(std::as_const(opt)).value());
+}
+
+constexpr void test_ref() {
+ {
+ X x;
+ optional<X&> opt{x};
+ assert(opt.value().test() == 4);
+ assert(std::as_const(opt).value().test() == 4);
+ assert(std::move(opt).value().test() == 4);
+ assert(std::move(std::as_const(opt)).value().test() == 4);
+ }
+ {
+ X x;
+ optional<const X&> opt{x};
+ assert(opt.value().test() == 3);
+ assert(std::as_const(opt).value().test() == 3);
+ assert(std::move(opt).value().test() == 3);
+ assert(std::move(std::as_const(opt)).value().test() == 3);
+ }
+}
+#endif
+
+constexpr bool test() {
+ test_contract<int>();
+ test_contract<float>();
+ test_contract<double>();
+ test_contract<X>();
+
+ test_contract<const int>();
+ test_contract<const float>();
+ test_contract<const double>();
+ test_contract<const X>();
+
+ {
+ std::optional<X> opt(X{});
+
+ assert(std::as_const(opt).value().test() == 3);
+ assert(opt.value().test() == 4);
+ assert(std::move(std::as_const(opt)).value().test() == 5);
+ assert(std::move(opt).value().test() == 6);
+ }
-int main(int, char**)
-{
- {
- optional<X> opt; ((void)opt);
- ASSERT_NOT_NOEXCEPT(opt.value());
- ASSERT_SAME_TYPE(decltype(opt.value()), X&);
- }
- {
- optional<X> opt;
- opt.emplace();
- assert(opt.value().test() == 4);
- }
#if TEST_STD_VER >= 26
- {
- X x;
- optional<X&> opt{x};
- ASSERT_NOT_NOEXCEPT(opt.value());
- ASSERT_SAME_TYPE(decltype(opt.value()), X&);
- }
+ test_ref_contract<int>();
+ test_ref_contract<float>();
+ test_ref_contract<double>();
+ test_ref_contract<X>();
+
+ test_ref_contract<const int>();
+ test_ref_contract<const float>();
+ test_ref_contract<const double>();
+ test_ref_contract<const X>();
+
+ test_ref();
#endif
+
+ return true;
+}
+
+int main(int, char**) {
+ assert(test());
+ static_assert(test());
#ifndef TEST_HAS_NO_EXCEPTIONS
- {
- optional<X> opt;
- try
- {
- (void)opt.value();
- assert(false);
- }
- catch (const bad_optional_access&)
- {
- }
+ {
+ optional<X> opt;
+ try {
+ (void)opt.value();
+ assert(false);
+ } catch (const bad_optional_access&) {
+ }
+
+ try {
+ (void)std::as_const(opt).value();
+ assert(false);
+ } catch (const bad_optional_access&) {
+ }
+
+ try {
+ (void)std::move(opt).value();
+ assert(false);
+ } catch (const bad_optional_access&) {
+ }
+
+ try {
+ (void)std::move(std::as_const(opt)).value();
+ assert(false);
+ } catch (const bad_optional_access&) {
+ }
+ }
+
+ {
+ optional<X> opt(X{});
+ try {
+ (void)opt.value();
+ } catch (const bad_optional_access&) {
+ assert(false);
+ }
+
+ try {
+ (void)std::as_const(opt).value();
+ } catch (const bad_optional_access&) {
+ assert(false);
+ }
+
+ try {
+ (void)std::move(opt).value();
+ } catch (const bad_optional_access&) {
+ assert(false);
+ }
+
+ try {
+ (void)std::move(std::as_const(opt)).value();
+ } catch (const bad_optional_access&) {
+ assert(false);
}
+ }
#endif
- static_assert(test() == 7, "");
return 0;
}
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.compile.fail.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.compile.fail.cpp
deleted file mode 100644
index 6dad6e3c6c6e4..0000000000000
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.compile.fail.cpp
+++ /dev/null
@@ -1,34 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <optional>
-
-// constexpr const T& optional<T>::value() const &;
-
-#include <optional>
-#include <type_traits>
-#include <cassert>
-
-using std::optional;
-
-struct X
-{
- constexpr int test() const {return 3;}
- int test() {return 4;}
-};
-
-int main(int, char**)
-{
- {
- constexpr optional<X> opt;
- static_assert(opt.value().test() == 3, "");
- }
-
- return 0;
-}
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.pass.cpp
deleted file mode 100644
index 044134126a374..0000000000000
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.pass.cpp
+++ /dev/null
@@ -1,106 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-
-// <optional>
-
-// constexpr const T& optional<T>::value() const &;
-
-#include <cassert>
-#include <memory>
-#include <optional>
-
-#include "test_macros.h"
-#if TEST_STD_VER >= 26
-# include "copy_move_types.h"
-#endif
-
-using std::optional;
-using std::in_place_t;
-using std::in_place;
-using std::bad_optional_access;
-
-struct X
-{
- X() = default;
- X(const X&) = delete;
- constexpr int test() const & {return 3;}
- int test() & {return 4;}
- constexpr int test() const && {return 5;}
- int test() && {return 6;}
-};
-
-#if TEST_STD_VER >= 26
-constexpr bool test_ref() {
- {
- TracedCopyMove x{};
- const std::optional<TracedCopyMove&> opt(x);
- ASSERT_NOT_NOEXCEPT(opt.value());
- ASSERT_SAME_TYPE(decltype(opt.value()), TracedCopyMove&);
-
- assert(std::addressof(opt.value()) == std::addressof(x));
- assert(opt->constMove == 0);
- assert(opt->nonConstMove == 0);
- assert(opt->constCopy == 0);
- assert(opt->nonConstCopy == 0);
- }
-
- {
- TracedCopyMove x{};
- const std::optional<const TracedCopyMove&> opt(x);
- ASSERT_NOT_NOEXCEPT(opt.value());
- ASSERT_SAME_TYPE(decltype(opt.value()), const TracedCopyMove&);
-
- assert(std::addressof(opt.value()) == std::addressof(x));
- assert(opt->constMove == 0);
- assert(opt->nonConstMove == 0);
- assert(opt->constCopy == 0);
- assert(opt->nonConstCopy == 0);
- }
-
- return true;
-}
-#endif
-
-int main(int, char**)
-{
- {
- const optional<X> opt; ((void)opt);
- ASSERT_NOT_NOEXCEPT(opt.value());
- ASSERT_SAME_TYPE(decltype(opt.value()), X const&);
- }
- {
- constexpr optional<X> opt(in_place);
- static_assert(opt.value().test() == 3, "");
- }
- {
- const optional<X> opt(in_place);
- assert(opt.value().test() == 3);
- }
-#ifndef TEST_HAS_NO_EXCEPTIONS
- {
- const optional<X> opt;
- try
- {
- (void)opt.value();
- assert(false);
- }
- catch (const bad_optional_access&)
- {
- }
- }
-#endif
-
-#if TEST_STD_VER >= 26
- assert(test_ref());
- static_assert(test_ref());
-#endif
-
- return 0;
-}
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const_rvalue.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const_rvalue.pass.cpp
deleted file mode 100644
index 66911bf85f5fe..0000000000000
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const_rvalue.pass.cpp
+++ /dev/null
@@ -1,106 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-
-// <optional>
-
-// constexpr const T& optional<T>::value() const &&;
-
-#include <cassert>
-#include <optional>
-#include <type_traits>
-
-#include "test_macros.h"
-#if TEST_STD_VER >= 26
-# include "copy_move_types.h"
-#endif
-
-using std::optional;
-using std::in_place_t;
-using std::in_place;
-using std::bad_optional_access;
-
-struct X
-{
- X() = default;
- X(const X&) = delete;
- constexpr int test() const & {return 3;}
- int test() & {return 4;}
- constexpr int test() const && {return 5;}
- int test() && {return 6;}
-};
-
-#if TEST_STD_VER >= 26
-constexpr bool test_ref() {
- {
- TracedCopyMove x{};
- const std::optional<TracedCopyMove&> opt(x);
- ASSERT_NOT_NOEXCEPT(std::move(opt).value());
- ASSERT_SAME_TYPE(decltype(std::move(opt).value()), TracedCopyMove&);
-
- assert(std::addressof(std::move(opt).value()) == std::addressof(x));
- auto& val = std::move(opt).value();
- assert(val.constMove == 0);
- assert(val.nonConstMove == 0);
- assert(val.constCopy == 0);
- assert(val.nonConstCopy == 0);
- }
-
- {
- TracedCopyMove x{};
- const std::optional<const TracedCopyMove&> opt(x);
- ASSERT_NOT_NOEXCEPT(std::move(opt).value());
- ASSERT_SAME_TYPE(decltype(std::move(opt).value()), const TracedCopyMove&);
-
- assert(std::addressof(std::move(opt).value()) == std::addressof(x));
- auto& val = std::move(opt).value();
- assert(val.constMove == 0);
- assert(val.nonConstMove == 0);
- assert(val.constCopy == 0);
- assert(val.nonConstCopy == 0);
- }
-
- return true;
-}
-#endif
-
-int main(int, char**)
-{
- {
- const optional<X> opt; ((void)opt);
- ASSERT_NOT_NOEXCEPT(std::move(opt).value());
- ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X const&&);
- }
- {
- constexpr optional<X> opt(in_place);
- static_assert(std::move(opt).value().test() == 5, "");
- }
- {
- const optional<X> opt(in_place);
- assert(std::move(opt).value().test() == 5);
- }
-#ifndef TEST_HAS_NO_EXCEPTIONS
- {
- const optional<X> opt;
- try
- {
- (void)std::move(opt).value();
- assert(false);
- }
- catch (const bad_optional_access&)
- {
- }
- }
-#endif
-#if TEST_STD_VER >= 26
- assert(test_ref());
- static_assert(test_ref());
-#endif
- return 0;
-}
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_rvalue.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_rvalue.pass.cpp
deleted file mode 100644
index 5de0187b49572..0000000000000
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_rvalue.pass.cpp
+++ /dev/null
@@ -1,73 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <optional>
-
-// constexpr T& optional<T>::value() &&;
-
-#include <optional>
-#include <type_traits>
-#include <cassert>
-
-#include "test_macros.h"
-
-using std::optional;
-using std::bad_optional_access;
-
-struct X
-{
- X() = default;
- X(const X&) = delete;
- constexpr int test() const & {return 3;}
- int test() & {return 4;}
- constexpr int test() const && {return 5;}
- int test() && {return 6;}
-};
-
-struct Y
-{
- constexpr int test() && {return 7;}
-};
-
-constexpr int
-test()
-{
- optional<Y> opt{Y{}};
- return std::move(opt).value().test();
-}
-
-int main(int, char**)
-{
- {
- optional<X> opt; ((void)opt);
- ASSERT_NOT_NOEXCEPT(std::move(opt).value());
- ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X&&);
- }
- {
- optional<X> opt;
- opt.emplace();
- assert(std::move(opt).value().test() == 6);
- }
-#ifndef TEST_HAS_NO_EXCEPTIONS
- {
- optional<X> opt;
- try
- {
- (void)std::move(opt).value();
- assert(false);
- }
- catch (const bad_optional_access&)
- {
- }
- }
-#endif
- static_assert(test() == 7, "");
-
- return 0;
-}
>From e2a9d8dbb7ac0ac7815fde11120f52e1de84d175 Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Mon, 12 Jan 2026 21:07:14 -0500
Subject: [PATCH 4/5] Missing header
---
.../optional.object/optional.object.observe/dereference.pass.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
index acd900a3d6638..eaed96daeb37e 100644
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
@@ -18,6 +18,7 @@
#include <cassert>
#include <memory>
#include <optional>
+#include <utility>
#include "test_macros.h"
#if TEST_STD_VER >= 26
>From 1ea3354681e0de601b400244965ea7fbef4d9d14 Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Tue, 13 Jan 2026 19:54:45 -0500
Subject: [PATCH 5/5] Address comments
---
.../dereference.pass.cpp | 69 ++++++++----------
.../optional.object.observe/op_arrow.pass.cpp | 70 +++++++++----------
.../optional.object.observe/value.pass.cpp | 59 +++++++---------
3 files changed, 90 insertions(+), 108 deletions(-)
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
index eaed96daeb37e..48ea98e4fe36e 100644
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp
@@ -25,8 +25,6 @@
# include "copy_move_types.h"
#endif
-using std::optional;
-
struct X {
constexpr int test() const& { return 3; }
constexpr int test() & { return 4; }
@@ -63,41 +61,6 @@ constexpr void test_ref_contract() {
ASSERT_NOEXCEPT(*std::as_const(opt));
ASSERT_NOEXCEPT(*std::move(std::as_const(opt)));
}
-
-constexpr void test_ref() {
- {
- TracedCopyMove x{};
- std::optional<TracedCopyMove&> opt(x);
-
- assert(std::addressof(*opt) == std::addressof(x));
- assert(std::addressof(*std::move(opt)) == std::addressof(x));
- assert(std::addressof(*std::as_const(opt)) == std::addressof(x));
- assert(std::addressof(*std::move(std::as_const(opt))) == std::addressof(x));
- assert(x.constMove == 0);
- assert(x.nonConstMove == 0);
- assert(x.constCopy == 0);
- assert(x.nonConstCopy == 0);
- }
-
- {
- TracedCopyMove x{};
- std::optional<const TracedCopyMove&> opt(x);
-
- assert(std::addressof(*opt) == std::addressof(x));
- assert(std::addressof(*std::move(opt)) == std::addressof(x));
- assert(std::addressof(*std::as_const(opt)) == std::addressof(x));
- assert(std::addressof(*std::move(std::as_const(opt))) == std::addressof(x));
- assert(x.constMove == 0);
- assert(x.nonConstMove == 0);
- assert(x.constCopy == 0);
- assert(x.nonConstCopy == 0);
- }
- {
- X x{};
- optional<X&> opt(x);
- assert((*opt).test() == 4);
- }
-}
#endif
constexpr bool test() {
@@ -128,7 +91,37 @@ constexpr bool test() {
test_ref_contract<const double>();
test_ref_contract<const X>();
- test_ref();
+ {
+ TracedCopyMove x{};
+ std::optional<TracedCopyMove&> o1(x);
+
+ assert(std::addressof(*o1) == std::addressof(x));
+ assert(std::addressof(*std::move(o1)) == std::addressof(x));
+ assert(std::addressof(*std::as_const(o1)) == std::addressof(x));
+ assert(std::addressof(*std::move(std::as_const(o1))) == std::addressof(x));
+ assert(x.constMove == 0);
+ assert(x.nonConstMove == 0);
+ assert(x.constCopy == 0);
+ assert(x.nonConstCopy == 0);
+ }
+ {
+ TracedCopyMove x{};
+ std::optional<const TracedCopyMove&> o2(x);
+
+ assert(std::addressof(*o2) == std::addressof(x));
+ assert(std::addressof(*std::move(o2)) == std::addressof(x));
+ assert(std::addressof(*std::as_const(o2)) == std::addressof(x));
+ assert(std::addressof(*std::move(std::as_const(o2))) == std::addressof(x));
+ assert(x.constMove == 0);
+ assert(x.nonConstMove == 0);
+ assert(x.constCopy == 0);
+ assert(x.nonConstCopy == 0);
+ }
+ {
+ X x{};
+ std::optional<X&> o3(x);
+ assert((*o3).test() == 4);
+ }
#endif
return true;
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp
index a3e58183a2d60..9392cc1bacc06 100644
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp
@@ -20,8 +20,6 @@
#include "test_macros.h"
-using std::optional;
-
struct X {
constexpr int test() noexcept { return 3; }
constexpr int test() const noexcept { return 4; }
@@ -49,40 +47,7 @@ constexpr void test_ref_contract() {
ASSERT_NOEXCEPT(std::as_const(opt).operator->());
}
-constexpr void test_ref() {
- {
- X x{};
- std::optional<X&> opt(x);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
- ASSERT_NOEXCEPT(opt.operator->());
- assert(opt.operator->() == std::addressof(x));
- assert(opt->test() == 3);
- }
- {
- X x{};
- std::optional<const X&> opt(x);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), const X*);
- ASSERT_NOEXCEPT(opt.operator->());
- assert(opt.operator->() == std::addressof(x));
- assert(opt->test() == 4);
- }
- {
- X x{};
- const std::optional<X&> opt(x);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
- ASSERT_NOEXCEPT(opt.operator->());
- assert(opt.operator->() == std::addressof(x));
- assert(opt->test() == 3);
- }
- {
- X x{};
- const std::optional<const X&> opt(x);
- ASSERT_SAME_TYPE(decltype(opt.operator->()), const X*);
- ASSERT_NOEXCEPT(opt.operator->());
- assert(opt.operator->() == std::addressof(x));
- assert(opt->test() == 4);
- }
-}
+constexpr void test_ref() {}
#endif
@@ -115,7 +80,38 @@ constexpr bool test() {
test_ref_contract<const float>();
test_ref_contract<const double>();
- test_ref();
+ {
+ X x{};
+ std::optional<X&> o1(x);
+ ASSERT_SAME_TYPE(decltype(o1.operator->()), X*);
+ ASSERT_NOEXCEPT(o1.operator->());
+ assert(o1.operator->() == std::addressof(x));
+ assert(o1->test() == 3);
+ }
+ {
+ X x{};
+ std::optional<const X&> o2(x);
+ ASSERT_SAME_TYPE(decltype(o2.operator->()), const X*);
+ ASSERT_NOEXCEPT(o2.operator->());
+ assert(o2.operator->() == std::addressof(x));
+ assert(o2->test() == 4);
+ }
+ {
+ X x{};
+ const std::optional<X&> o3(x);
+ ASSERT_SAME_TYPE(decltype(o3.operator->()), X*);
+ ASSERT_NOEXCEPT(o3.operator->());
+ assert(o3.operator->() == std::addressof(x));
+ assert(o3->test() == 3);
+ }
+ {
+ X x{};
+ const std::optional<const X&> o4(x);
+ ASSERT_SAME_TYPE(decltype(o4.operator->()), const X*);
+ ASSERT_NOEXCEPT(o4.operator->());
+ assert(o4.operator->() == std::addressof(x));
+ assert(o4->test() == 4);
+ }
#endif
return true;
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp
index 0bea13cfc5f82..93fbfaed52d25 100644
--- a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp
@@ -22,9 +22,6 @@
#include "test_macros.h"
-using std::bad_optional_access;
-using std::optional;
-
struct X {
constexpr X() = default;
constexpr int test() const& { return 3; }
@@ -63,25 +60,6 @@ constexpr void test_ref_contract() {
ASSERT_NOT_NOEXCEPT(std::move(opt).value());
ASSERT_NOT_NOEXCEPT(std::move(std::as_const(opt)).value());
}
-
-constexpr void test_ref() {
- {
- X x;
- optional<X&> opt{x};
- assert(opt.value().test() == 4);
- assert(std::as_const(opt).value().test() == 4);
- assert(std::move(opt).value().test() == 4);
- assert(std::move(std::as_const(opt)).value().test() == 4);
- }
- {
- X x;
- optional<const X&> opt{x};
- assert(opt.value().test() == 3);
- assert(std::as_const(opt).value().test() == 3);
- assert(std::move(opt).value().test() == 3);
- assert(std::move(std::as_const(opt)).value().test() == 3);
- }
-}
#endif
constexpr bool test() {
@@ -115,7 +93,22 @@ constexpr bool test() {
test_ref_contract<const double>();
test_ref_contract<const X>();
- test_ref();
+ {
+ X x;
+ std::optional<X&> o1{x};
+ assert(o1.value().test() == 4);
+ assert(std::as_const(o1).value().test() == 4);
+ assert(std::move(o1).value().test() == 4);
+ assert(std::move(std::as_const(o1)).value().test() == 4);
+ }
+ {
+ X x;
+ std::optional<const X&> o2{x};
+ assert(o2.value().test() == 3);
+ assert(std::as_const(o2).value().test() == 3);
+ assert(std::move(o2).value().test() == 3);
+ assert(std::move(std::as_const(o2)).value().test() == 3);
+ }
#endif
return true;
@@ -126,55 +119,55 @@ int main(int, char**) {
static_assert(test());
#ifndef TEST_HAS_NO_EXCEPTIONS
{
- optional<X> opt;
+ std::optional<X> opt;
try {
(void)opt.value();
assert(false);
- } catch (const bad_optional_access&) {
+ } catch (const std::bad_optional_access&) {
}
try {
(void)std::as_const(opt).value();
assert(false);
- } catch (const bad_optional_access&) {
+ } catch (const std::bad_optional_access&) {
}
try {
(void)std::move(opt).value();
assert(false);
- } catch (const bad_optional_access&) {
+ } catch (const std::bad_optional_access&) {
}
try {
(void)std::move(std::as_const(opt)).value();
assert(false);
- } catch (const bad_optional_access&) {
+ } catch (const std::bad_optional_access&) {
}
}
{
- optional<X> opt(X{});
+ std::optional<X> opt(X{});
try {
(void)opt.value();
- } catch (const bad_optional_access&) {
+ } catch (const std::bad_optional_access&) {
assert(false);
}
try {
(void)std::as_const(opt).value();
- } catch (const bad_optional_access&) {
+ } catch (const std::bad_optional_access&) {
assert(false);
}
try {
(void)std::move(opt).value();
- } catch (const bad_optional_access&) {
+ } catch (const std::bad_optional_access&) {
assert(false);
}
try {
(void)std::move(std::as_const(opt)).value();
- } catch (const bad_optional_access&) {
+ } catch (const std::bad_optional_access&) {
assert(false);
}
}
More information about the libcxx-commits
mailing list