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 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: c++03, c++11, c++14
10
11 // Throwing bad_optional_access is supported starting in macosx10.13
12 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
13
14 // <optional>
15
16 // constexpr optional(T&& v);
17
18 #include <optional>
19 #include <type_traits>
20 #include <cassert>
21
22 #include "test_macros.h"
23 #include "archetypes.h"
24
25
26 using std::optional;
27
28
29 class Z
30 {
31 public:
Z(int)32 Z(int) {}
Z(Z &&)33 Z(Z&&) {TEST_THROW(6);}
34 };
35
36
main(int,char **)37 int main(int, char**)
38 {
39 {
40 typedef int T;
41 constexpr optional<T> opt(T(5));
42 static_assert(static_cast<bool>(opt) == true, "");
43 static_assert(*opt == 5, "");
44
45 struct test_constexpr_ctor
46 : public optional<T>
47 {
48 constexpr test_constexpr_ctor(T&&) {}
49 };
50 }
51 {
52 typedef double T;
53 constexpr optional<T> opt(T(3));
54 static_assert(static_cast<bool>(opt) == true, "");
55 static_assert(*opt == 3, "");
56
57 struct test_constexpr_ctor
58 : public optional<T>
59 {
60 constexpr test_constexpr_ctor(T&&) {}
61 };
62 }
63 {
64 const int x = 42;
65 optional<const int> o(std::move(x));
66 assert(*o == 42);
67 }
68 {
69 typedef TestTypes::TestType T;
70 T::reset();
71 optional<T> opt = T{3};
72 assert(T::alive == 1);
73 assert(T::move_constructed == 1);
74 assert(static_cast<bool>(opt) == true);
75 assert(opt.value().value == 3);
76 }
77 {
78 typedef ExplicitTestTypes::TestType T;
79 static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
80 T::reset();
81 optional<T> opt(T{3});
82 assert(T::alive == 1);
83 assert(T::move_constructed == 1);
84 assert(static_cast<bool>(opt) == true);
85 assert(opt.value().value == 3);
86 }
87 {
88 typedef TestTypes::TestType T;
89 T::reset();
90 optional<T> opt = {3};
91 assert(T::alive == 1);
92 assert(T::value_constructed == 1);
93 assert(T::copy_constructed == 0);
94 assert(T::move_constructed == 0);
95 assert(static_cast<bool>(opt) == true);
96 assert(opt.value().value == 3);
97 }
98 {
99 typedef ConstexprTestTypes::TestType T;
100 constexpr optional<T> opt = {T(3)};
101 static_assert(static_cast<bool>(opt) == true, "");
102 static_assert(opt.value().value == 3, "");
103
104 struct test_constexpr_ctor
105 : public optional<T>
106 {
107 constexpr test_constexpr_ctor(const T&) {}
108 };
109 }
110 {
111 typedef ConstexprTestTypes::TestType T;
112 constexpr optional<T> opt = {3};
113 static_assert(static_cast<bool>(opt) == true, "");
114 static_assert(opt.value().value == 3, "");
115
116 struct test_constexpr_ctor
117 : public optional<T>
118 {
119 constexpr test_constexpr_ctor(const T&) {}
120 };
121 }
122 {
123 typedef ExplicitConstexprTestTypes::TestType T;
124 static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
125 constexpr optional<T> opt(T{3});
126 static_assert(static_cast<bool>(opt) == true, "");
127 static_assert(opt.value().value == 3, "");
128
129 struct test_constexpr_ctor
130 : public optional<T>
131 {
132 constexpr test_constexpr_ctor(T&&) {}
133 };
134
135 }
136 #ifndef TEST_HAS_NO_EXCEPTIONS
137 {
138 try
139 {
140 Z z(3);
141 optional<Z> opt(std::move(z));
142 assert(false);
143 }
144 catch (int i)
145 {
146 assert(i == 6);
147 }
148 }
149 #endif
150
151 return 0;
152 }
153