Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / optional / optional.object / optional.object.ctor / explicit_optional_U.pass.cpp
blob708370a47b616b7b0429790f6c8eb6820932daf5
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 // explicit optional(optional<U>&& rhs);
15 #include <optional>
16 #include <type_traits>
17 #include <cassert>
19 #include "test_macros.h"
21 using std::optional;
23 template <class T, class U>
24 TEST_CONSTEXPR_CXX20 void test(optional<U>&& rhs, bool is_going_to_throw = false)
26 static_assert(!(std::is_convertible<optional<U>&&, optional<T>>::value), "");
27 bool rhs_engaged = static_cast<bool>(rhs);
28 #ifndef TEST_HAS_NO_EXCEPTIONS
29 try
31 optional<T> lhs(std::move(rhs));
32 assert(is_going_to_throw == false);
33 assert(static_cast<bool>(lhs) == rhs_engaged);
35 catch (int i)
37 assert(i == 6);
39 #else
40 if (is_going_to_throw) return;
41 optional<T> lhs(std::move(rhs));
42 assert(static_cast<bool>(lhs) == rhs_engaged);
43 #endif
46 class X
48 int i_;
49 public:
50 constexpr explicit X(int i) : i_(i) {}
51 constexpr X(X&& x) : i_(x.i_) { x.i_ = 0; }
52 TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;}
53 friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
56 int count = 0;
58 class Z
60 public:
61 explicit Z(int) { TEST_THROW(6); }
64 TEST_CONSTEXPR_CXX20 bool test()
67 optional<int> rhs;
68 test<X>(std::move(rhs));
71 optional<int> rhs(3);
72 test<X>(std::move(rhs));
75 return true;
78 int main(int, char**)
80 #if TEST_STD_VER > 17
81 static_assert(test());
82 #endif
83 test();
85 optional<int> rhs;
86 test<Z>(std::move(rhs));
89 optional<int> rhs(3);
90 test<Z>(std::move(rhs), true);
93 return 0;