1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
13 // optional(optional<U>&& rhs);
18 #include <type_traits>
21 #include "test_macros.h"
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
33 optional
<T
> lhs
= std::move(rhs
);
34 assert(is_going_to_throw
== false);
35 assert(static_cast<bool>(lhs
) == rhs_engaged
);
42 if (is_going_to_throw
) return;
43 optional
<T
> lhs
= std::move(rhs
);
44 assert(static_cast<bool>(lhs
) == rhs_engaged
);
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_
;}
60 Z(int) { TEST_THROW(6); }
63 template<class T
, class U
>
64 TEST_CONSTEXPR_CXX20
bool test_all()
68 test
<U
>(std::move(rhs
));
71 optional
<T
> rhs(short{3});
72 test
<U
>(std::move(rhs
));
79 test_all
<short, int>();
82 static_assert(test_all
<short, int>());
83 static_assert(test_all
<int, X
>());
87 test
<Z
>(std::move(rhs
));
91 test
<Z
>(std::move(rhs
), true);
94 static_assert(!(std::is_constructible
<optional
<X
>, optional
<Z
>>::value
), "");