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
11 // XFAIL: availability-bad_optional_access-missing && !no-exceptions
15 // constexpr optional(T&& v);
18 #include <type_traits>
21 #include "test_macros.h"
22 #include "archetypes.h"
32 Z(Z
&&) {TEST_THROW(6);}
40 constexpr optional
<T
> opt(T(5));
41 static_assert(static_cast<bool>(opt
) == true, "");
42 static_assert(*opt
== 5, "");
44 struct test_constexpr_ctor
47 constexpr test_constexpr_ctor(T
&&) {}
52 constexpr optional
<T
> opt(T(3));
53 static_assert(static_cast<bool>(opt
) == true, "");
54 static_assert(*opt
== 3, "");
56 struct test_constexpr_ctor
59 constexpr test_constexpr_ctor(T
&&) {}
64 optional
<const int> o(std::move(x
));
68 typedef TestTypes::TestType T
;
70 optional
<T
> opt
= T
{3};
71 assert(T::alive
== 1);
72 assert(T::move_constructed
== 1);
73 assert(static_cast<bool>(opt
) == true);
74 assert(opt
.value().value
== 3);
77 typedef ExplicitTestTypes::TestType T
;
78 static_assert(!std::is_convertible
<T
&&, optional
<T
>>::value
, "");
80 optional
<T
> opt(T
{3});
81 assert(T::alive
== 1);
82 assert(T::move_constructed
== 1);
83 assert(static_cast<bool>(opt
) == true);
84 assert(opt
.value().value
== 3);
87 typedef TestTypes::TestType T
;
89 optional
<T
> opt
= {3};
90 assert(T::alive
== 1);
91 assert(T::value_constructed
== 1);
92 assert(T::copy_constructed
== 0);
93 assert(T::move_constructed
== 0);
94 assert(static_cast<bool>(opt
) == true);
95 assert(opt
.value().value
== 3);
98 typedef ConstexprTestTypes::TestType T
;
99 constexpr optional
<T
> opt
= {T(3)};
100 static_assert(static_cast<bool>(opt
) == true, "");
101 static_assert(opt
.value().value
== 3, "");
103 struct test_constexpr_ctor
106 constexpr test_constexpr_ctor(const T
&) {}
110 typedef ConstexprTestTypes::TestType T
;
111 constexpr optional
<T
> opt
= {3};
112 static_assert(static_cast<bool>(opt
) == true, "");
113 static_assert(opt
.value().value
== 3, "");
115 struct test_constexpr_ctor
118 constexpr test_constexpr_ctor(const T
&) {}
122 typedef ExplicitConstexprTestTypes::TestType T
;
123 static_assert(!std::is_convertible
<T
&&, optional
<T
>>::value
, "");
124 constexpr optional
<T
> opt(T
{3});
125 static_assert(static_cast<bool>(opt
) == true, "");
126 static_assert(opt
.value().value
== 3, "");
128 struct test_constexpr_ctor
131 constexpr test_constexpr_ctor(T
&&) {}
135 #ifndef TEST_HAS_NO_EXCEPTIONS
140 optional
<Z
> opt(std::move(z
));