emplace.pass.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // <optional>
  10. // template <class... Args> T& optional<T>::emplace(Args&&... args);
  11. #include <optional>
  12. #include <type_traits>
  13. #include <cassert>
  14. #include <memory>
  15. #include "test_macros.h"
  16. #include "archetypes.h"
  17. using std::optional;
  18. class X
  19. {
  20. int i_;
  21. int j_ = 0;
  22. public:
  23. X() : i_(0) {}
  24. X(int i) : i_(i) {}
  25. X(int i, int j) : i_(i), j_(j) {}
  26. friend bool operator==(const X& x, const X& y)
  27. {return x.i_ == y.i_ && x.j_ == y.j_;}
  28. };
  29. class Y
  30. {
  31. public:
  32. static bool dtor_called;
  33. Y() = default;
  34. Y(int) { TEST_THROW(6);}
  35. ~Y() {dtor_called = true;}
  36. };
  37. bool Y::dtor_called = false;
  38. template <class T>
  39. void test_one_arg() {
  40. using Opt = std::optional<T>;
  41. {
  42. Opt opt;
  43. auto & v = opt.emplace();
  44. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  45. assert(static_cast<bool>(opt) == true);
  46. assert(*opt == T(0));
  47. assert(&v == &*opt);
  48. }
  49. {
  50. Opt opt;
  51. auto & v = opt.emplace(1);
  52. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  53. assert(static_cast<bool>(opt) == true);
  54. assert(*opt == T(1));
  55. assert(&v == &*opt);
  56. }
  57. {
  58. Opt opt(2);
  59. auto & v = opt.emplace();
  60. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  61. assert(static_cast<bool>(opt) == true);
  62. assert(*opt == T(0));
  63. assert(&v == &*opt);
  64. }
  65. {
  66. Opt opt(2);
  67. auto & v = opt.emplace(1);
  68. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  69. assert(static_cast<bool>(opt) == true);
  70. assert(*opt == T(1));
  71. assert(&v == &*opt);
  72. }
  73. }
  74. template <class T>
  75. void test_multi_arg()
  76. {
  77. test_one_arg<T>();
  78. using Opt = std::optional<T>;
  79. {
  80. Opt opt;
  81. auto &v = opt.emplace(101, 41);
  82. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  83. assert(static_cast<bool>(opt) == true);
  84. assert( v == T(101, 41));
  85. assert(*opt == T(101, 41));
  86. }
  87. {
  88. Opt opt;
  89. auto &v = opt.emplace({1, 2, 3, 4});
  90. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  91. assert(static_cast<bool>(opt) == true);
  92. assert( v == T(4)); // T sets its value to the size of the init list
  93. assert(*opt == T(4));
  94. }
  95. {
  96. Opt opt;
  97. auto &v = opt.emplace({1, 2, 3, 4, 5}, 6);
  98. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  99. assert(static_cast<bool>(opt) == true);
  100. assert( v == T(5)); // T sets its value to the size of the init list
  101. assert(*opt == T(5)); // T sets its value to the size of the init list
  102. }
  103. }
  104. template <class T>
  105. void test_on_test_type() {
  106. T::reset();
  107. optional<T> opt;
  108. assert(T::alive == 0);
  109. {
  110. T::reset_constructors();
  111. auto &v = opt.emplace();
  112. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  113. assert(T::alive == 1);
  114. assert(T::constructed == 1);
  115. assert(T::default_constructed == 1);
  116. assert(T::destroyed == 0);
  117. assert(static_cast<bool>(opt) == true);
  118. assert(*opt == T());
  119. assert(&v == &*opt);
  120. }
  121. {
  122. T::reset_constructors();
  123. auto &v = opt.emplace();
  124. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  125. assert(T::alive == 1);
  126. assert(T::constructed == 1);
  127. assert(T::default_constructed == 1);
  128. assert(T::destroyed == 1);
  129. assert(static_cast<bool>(opt) == true);
  130. assert(*opt == T());
  131. assert(&v == &*opt);
  132. }
  133. {
  134. T::reset_constructors();
  135. auto &v = opt.emplace(101);
  136. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  137. assert(T::alive == 1);
  138. assert(T::constructed == 1);
  139. assert(T::value_constructed == 1);
  140. assert(T::destroyed == 1);
  141. assert(static_cast<bool>(opt) == true);
  142. assert(*opt == T(101));
  143. assert(&v == &*opt);
  144. }
  145. {
  146. T::reset_constructors();
  147. auto &v = opt.emplace(-10, 99);
  148. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  149. assert(T::alive == 1);
  150. assert(T::constructed == 1);
  151. assert(T::value_constructed == 1);
  152. assert(T::destroyed == 1);
  153. assert(static_cast<bool>(opt) == true);
  154. assert(*opt == T(-10, 99));
  155. assert(&v == &*opt);
  156. }
  157. {
  158. T::reset_constructors();
  159. auto &v = opt.emplace(-10, 99);
  160. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  161. assert(T::alive == 1);
  162. assert(T::constructed == 1);
  163. assert(T::value_constructed == 1);
  164. assert(T::destroyed == 1);
  165. assert(static_cast<bool>(opt) == true);
  166. assert(*opt == T(-10, 99));
  167. assert(&v == &*opt);
  168. }
  169. {
  170. T::reset_constructors();
  171. auto &v = opt.emplace({-10, 99, 42, 1});
  172. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  173. assert(T::alive == 1);
  174. assert(T::constructed == 1);
  175. assert(T::value_constructed == 1);
  176. assert(T::destroyed == 1);
  177. assert(static_cast<bool>(opt) == true);
  178. assert(*opt == T(4)); // size of the initializer list
  179. assert(&v == &*opt);
  180. }
  181. {
  182. T::reset_constructors();
  183. auto &v = opt.emplace({-10, 99, 42, 1}, 42);
  184. static_assert( std::is_same_v<T&, decltype(v)>, "" );
  185. assert(T::alive == 1);
  186. assert(T::constructed == 1);
  187. assert(T::value_constructed == 1);
  188. assert(T::destroyed == 1);
  189. assert(static_cast<bool>(opt) == true);
  190. assert(*opt == T(4)); // size of the initializer list
  191. assert(&v == &*opt);
  192. }
  193. }
  194. int main(int, char**)
  195. {
  196. {
  197. test_on_test_type<TestTypes::TestType>();
  198. test_on_test_type<ExplicitTestTypes::TestType>();
  199. }
  200. {
  201. using T = int;
  202. test_one_arg<T>();
  203. test_one_arg<const T>();
  204. }
  205. {
  206. using T = ConstexprTestTypes::TestType;
  207. test_multi_arg<T>();
  208. }
  209. {
  210. using T = ExplicitConstexprTestTypes::TestType;
  211. test_multi_arg<T>();
  212. }
  213. {
  214. using T = TrivialTestTypes::TestType;
  215. test_multi_arg<T>();
  216. }
  217. {
  218. using T = ExplicitTrivialTestTypes::TestType;
  219. test_multi_arg<T>();
  220. }
  221. {
  222. optional<const int> opt;
  223. auto &v = opt.emplace(42);
  224. static_assert( std::is_same_v<const int&, decltype(v)>, "" );
  225. assert(*opt == 42);
  226. assert( v == 42);
  227. opt.emplace();
  228. assert(*opt == 0);
  229. }
  230. #ifndef TEST_HAS_NO_EXCEPTIONS
  231. Y::dtor_called = false;
  232. {
  233. Y y;
  234. optional<Y> opt(y);
  235. try
  236. {
  237. assert(static_cast<bool>(opt) == true);
  238. assert(Y::dtor_called == false);
  239. auto &v = opt.emplace(1);
  240. static_assert( std::is_same_v<Y&, decltype(v)>, "" );
  241. assert(false);
  242. }
  243. catch (int i)
  244. {
  245. assert(i == 6);
  246. assert(static_cast<bool>(opt) == false);
  247. assert(Y::dtor_called == true);
  248. }
  249. }
  250. #endif
  251. return 0;
  252. }