Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / optional / optional.object / optional.object.ctor / optional_U.pass.cpp
blob14c400cdd15267d4dc800be91675e2ac629e0084
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
10 // <optional>
12 // template <class U>
13 // optional(optional<U>&& rhs);
15 #include <cassert>
16 #include <memory>
17 #include <optional>
18 #include <type_traits>
19 #include <utility>
21 #include "test_macros.h"
23 using std::optional;
25 template <class T, class U>
26 TEST_CONSTEXPR_CXX20 void
27 test(optional<U>&& rhs, bool is_going_to_throw = false)
29 bool rhs_engaged = static_cast<bool>(rhs);
30 #ifndef TEST_HAS_NO_EXCEPTIONS
31 try
33 optional<T> lhs = std::move(rhs);
34 assert(is_going_to_throw == false);
35 assert(static_cast<bool>(lhs) == rhs_engaged);
37 catch (int i)
39 assert(i == 6);
41 #else
42 if (is_going_to_throw) return;
43 optional<T> lhs = std::move(rhs);
44 assert(static_cast<bool>(lhs) == rhs_engaged);
45 #endif
48 class X
50 int i_;
51 public:
52 TEST_CONSTEXPR_CXX20 X(int i) : i_(i) {}
53 TEST_CONSTEXPR_CXX20 X(X&& x) : i_(std::exchange(x.i_, 0)) {}
54 TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;}
55 friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
58 struct Z
60 Z(int) { TEST_THROW(6); }
63 template<class T, class U>
64 TEST_CONSTEXPR_CXX20 bool test_all()
67 optional<T> rhs;
68 test<U>(std::move(rhs));
71 optional<T> rhs(short{3});
72 test<U>(std::move(rhs));
74 return true;
77 int main(int, char**)
79 test_all<short, int>();
80 test_all<int, X>();
81 #if TEST_STD_VER > 17
82 static_assert(test_all<short, int>());
83 static_assert(test_all<int, X>());
84 #endif
86 optional<int> rhs;
87 test<Z>(std::move(rhs));
90 optional<int> rhs(3);
91 test<Z>(std::move(rhs), true);
94 static_assert(!(std::is_constructible<optional<X>, optional<Z>>::value), "");
96 return 0;