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 // explicit optional(optional<U>&& rhs);
16 #include <type_traits>
19 #include "test_macros.h"
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
31 optional
<T
> lhs(std::move(rhs
));
32 assert(is_going_to_throw
== false);
33 assert(static_cast<bool>(lhs
) == rhs_engaged
);
40 if (is_going_to_throw
) return;
41 optional
<T
> lhs(std::move(rhs
));
42 assert(static_cast<bool>(lhs
) == rhs_engaged
);
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_
;}
61 explicit Z(int) { TEST_THROW(6); }
64 TEST_CONSTEXPR_CXX20
bool test()
68 test
<X
>(std::move(rhs
));
72 test
<X
>(std::move(rhs
));
81 static_assert(test());
86 test
<Z
>(std::move(rhs
));
90 test
<Z
>(std::move(rhs
), true);