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 // constexpr optional(T&& v);
16 #include <type_traits>
19 #include "test_macros.h"
20 #include "archetypes.h"
30 Z(Z
&&) {TEST_THROW(6);}
38 constexpr optional
<T
> opt(T(5));
39 static_assert(static_cast<bool>(opt
) == true, "");
40 static_assert(*opt
== 5, "");
42 struct test_constexpr_ctor
45 constexpr test_constexpr_ctor(T
&&) {}
50 constexpr optional
<T
> opt(T(3));
51 static_assert(static_cast<bool>(opt
) == true, "");
52 static_assert(*opt
== 3, "");
54 struct test_constexpr_ctor
57 constexpr test_constexpr_ctor(T
&&) {}
62 optional
<const int> o(std::move(x
));
66 typedef TestTypes::TestType T
;
68 optional
<T
> opt
= T
{3};
69 assert(T::alive
== 1);
70 assert(T::move_constructed
== 1);
71 assert(static_cast<bool>(opt
) == true);
72 assert(opt
.value().value
== 3);
75 typedef ExplicitTestTypes::TestType T
;
76 static_assert(!std::is_convertible
<T
&&, optional
<T
>>::value
, "");
78 optional
<T
> opt(T
{3});
79 assert(T::alive
== 1);
80 assert(T::move_constructed
== 1);
81 assert(static_cast<bool>(opt
) == true);
82 assert(opt
.value().value
== 3);
85 typedef TestTypes::TestType T
;
87 optional
<T
> opt
= {3};
88 assert(T::alive
== 1);
89 assert(T::value_constructed
== 1);
90 assert(T::copy_constructed
== 0);
91 assert(T::move_constructed
== 0);
92 assert(static_cast<bool>(opt
) == true);
93 assert(opt
.value().value
== 3);
96 typedef ConstexprTestTypes::TestType T
;
97 constexpr optional
<T
> opt
= {T(3)};
98 static_assert(static_cast<bool>(opt
) == true, "");
99 static_assert(opt
.value().value
== 3, "");
101 struct test_constexpr_ctor
104 constexpr test_constexpr_ctor(const T
&) {}
108 typedef ConstexprTestTypes::TestType T
;
109 constexpr optional
<T
> opt
= {3};
110 static_assert(static_cast<bool>(opt
) == true, "");
111 static_assert(opt
.value().value
== 3, "");
113 struct test_constexpr_ctor
116 constexpr test_constexpr_ctor(const T
&) {}
120 typedef ExplicitConstexprTestTypes::TestType T
;
121 static_assert(!std::is_convertible
<T
&&, optional
<T
>>::value
, "");
122 constexpr optional
<T
> opt(T
{3});
123 static_assert(static_cast<bool>(opt
) == true, "");
124 static_assert(opt
.value().value
== 3, "");
126 struct test_constexpr_ctor
129 constexpr test_constexpr_ctor(T
&&) {}
133 #ifndef TEST_HAS_NO_EXCEPTIONS
138 optional
<Z
> opt(std::move(z
));