1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
13 // template <class... Args> T& optional<T>::emplace(Args&&... args);
16 #include <type_traits>
20 #include "test_macros.h"
21 #include "archetypes.hpp"
32 X(int i, int j) : i_(i), j_(j) {}
34 friend bool operator==(const X& x, const X& y)
35 {return x.i_ == y.i_ && x.j_ == y.j_;}
41 static bool dtor_called;
43 Y(int) { TEST_THROW(6);}
44 ~Y() {dtor_called = true;}
47 bool Y::dtor_called = false;
51 using Opt = std::optional<T>;
54 auto & v = opt.emplace();
55 static_assert( std::is_same_v<T&, decltype(v)>, "" );
56 assert(static_cast<bool>(opt) == true);
62 auto & v = opt.emplace(1);
63 static_assert( std::is_same_v<T&, decltype(v)>, "" );
64 assert(static_cast<bool>(opt) == true);
70 auto & v = opt.emplace();
71 static_assert( std::is_same_v<T&, decltype(v)>, "" );
72 assert(static_cast<bool>(opt) == true);
78 auto & v = opt.emplace(1);
79 static_assert( std::is_same_v<T&, decltype(v)>, "" );
80 assert(static_cast<bool>(opt) == true);
91 using Opt = std::optional<T>;
94 auto &v = opt.emplace(101, 41);
95 static_assert( std::is_same_v<T&, decltype(v)>, "" );
96 assert(static_cast<bool>(opt) == true);
97 assert( v == T(101, 41));
98 assert(*opt == T(101, 41));
102 auto &v = opt.emplace({1, 2, 3, 4});
103 static_assert( std::is_same_v<T&, decltype(v)>, "" );
104 assert(static_cast<bool>(opt) == true);
105 assert( v == T(4)); // T sets its value to the size of the init list
106 assert(*opt == T(4));
110 auto &v = opt.emplace({1, 2, 3, 4, 5}, 6);
111 static_assert( std::is_same_v<T&, decltype(v)>, "" );
112 assert(static_cast<bool>(opt) == true);
113 assert( v == T(5)); // T sets its value to the size of the init list
114 assert(*opt == T(5)); // T sets its value to the size of the init list
119 void test_on_test_type() {
123 assert(T::alive == 0);
125 T::reset_constructors();
126 auto &v = opt.emplace();
127 static_assert( std::is_same_v<T&, decltype(v)>, "" );
128 assert(T::alive == 1);
129 assert(T::constructed == 1);
130 assert(T::default_constructed == 1);
131 assert(T::destroyed == 0);
132 assert(static_cast<bool>(opt) == true);
137 T::reset_constructors();
138 auto &v = opt.emplace();
139 static_assert( std::is_same_v<T&, decltype(v)>, "" );
140 assert(T::alive == 1);
141 assert(T::constructed == 1);
142 assert(T::default_constructed == 1);
143 assert(T::destroyed == 1);
144 assert(static_cast<bool>(opt) == true);
149 T::reset_constructors();
150 auto &v = opt.emplace(101);
151 static_assert( std::is_same_v<T&, decltype(v)>, "" );
152 assert(T::alive == 1);
153 assert(T::constructed == 1);
154 assert(T::value_constructed == 1);
155 assert(T::destroyed == 1);
156 assert(static_cast<bool>(opt) == true);
157 assert(*opt == T(101));
161 T::reset_constructors();
162 auto &v = opt.emplace(-10, 99);
163 static_assert( std::is_same_v<T&, decltype(v)>, "" );
164 assert(T::alive == 1);
165 assert(T::constructed == 1);
166 assert(T::value_constructed == 1);
167 assert(T::destroyed == 1);
168 assert(static_cast<bool>(opt) == true);
169 assert(*opt == T(-10, 99));
173 T::reset_constructors();
174 auto &v = opt.emplace(-10, 99);
175 static_assert( std::is_same_v<T&, decltype(v)>, "" );
176 assert(T::alive == 1);
177 assert(T::constructed == 1);
178 assert(T::value_constructed == 1);
179 assert(T::destroyed == 1);
180 assert(static_cast<bool>(opt) == true);
181 assert(*opt == T(-10, 99));
185 T::reset_constructors();
186 auto &v = opt.emplace({-10, 99, 42, 1});
187 static_assert( std::is_same_v<T&, decltype(v)>, "" );
188 assert(T::alive == 1);
189 assert(T::constructed == 1);
190 assert(T::value_constructed == 1);
191 assert(T::destroyed == 1);
192 assert(static_cast<bool>(opt) == true);
193 assert(*opt == T(4)); // size of the initializer list
197 T::reset_constructors();
198 auto &v = opt.emplace({-10, 99, 42, 1}, 42);
199 static_assert( std::is_same_v<T&, decltype(v)>, "" );
200 assert(T::alive == 1);
201 assert(T::constructed == 1);
202 assert(T::value_constructed == 1);
203 assert(T::destroyed == 1);
204 assert(static_cast<bool>(opt) == true);
205 assert(*opt == T(4)); // size of the initializer list
215 test_on_test_type<TestTypes::TestType>();
216 test_on_test_type<ExplicitTestTypes::TestType>();
221 test_one_arg<const T>();
224 using T = ConstexprTestTypes::TestType;
228 using T = ExplicitConstexprTestTypes::TestType;
232 using T = TrivialTestTypes::TestType;
236 using T = ExplicitTrivialTestTypes::TestType;
240 optional<const int> opt;
241 auto &v = opt.emplace(42);
242 static_assert( std::is_same_v<const int&, decltype(v)>, "" );
248 #ifndef TEST_HAS_NO_EXCEPTIONS
249 Y::dtor_called = false;
255 assert(static_cast<bool>(opt) == true);
256 assert(Y::dtor_called == false);
262 assert(static_cast<bool>(opt) == false);
263 assert(Y::dtor_called == true);