optional_U.pass.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // From LWG2451:
  11. // template <class U>
  12. // optional<T>& operator=(optional<U>&& rhs);
  13. #include <optional>
  14. #include <type_traits>
  15. #include <memory>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "archetypes.h"
  19. using std::optional;
  20. struct X
  21. {
  22. static bool throw_now;
  23. X() = default;
  24. X(int &&)
  25. {
  26. if (throw_now)
  27. TEST_THROW(6);
  28. }
  29. };
  30. bool X::throw_now = false;
  31. struct Y1
  32. {
  33. Y1() = default;
  34. Y1(const int&) {}
  35. Y1& operator=(const Y1&) = delete;
  36. };
  37. struct Y2
  38. {
  39. Y2() = default;
  40. Y2(const int&) = delete;
  41. Y2& operator=(const int&) { return *this; }
  42. };
  43. class B {};
  44. class D : public B {};
  45. template <class T>
  46. struct AssignableFrom {
  47. static int type_constructed;
  48. static int type_assigned;
  49. static int int_constructed;
  50. static int int_assigned;
  51. static void reset() {
  52. type_constructed = int_constructed = 0;
  53. type_assigned = int_assigned = 0;
  54. }
  55. AssignableFrom() = default;
  56. explicit AssignableFrom(T) { ++type_constructed; }
  57. AssignableFrom& operator=(T) { ++type_assigned; return *this; }
  58. AssignableFrom(int) { ++int_constructed; }
  59. AssignableFrom& operator=(int) { ++int_assigned; return *this; }
  60. private:
  61. AssignableFrom(AssignableFrom const&) = delete;
  62. AssignableFrom& operator=(AssignableFrom const&) = delete;
  63. };
  64. template <class T> int AssignableFrom<T>::type_constructed = 0;
  65. template <class T> int AssignableFrom<T>::type_assigned = 0;
  66. template <class T> int AssignableFrom<T>::int_constructed = 0;
  67. template <class T> int AssignableFrom<T>::int_assigned = 0;
  68. void test_with_test_type() {
  69. using T = TestTypes::TestType;
  70. T::reset();
  71. { // non-empty to empty
  72. T::reset_constructors();
  73. optional<T> opt;
  74. optional<int> other(42);
  75. opt = std::move(other);
  76. assert(T::alive == 1);
  77. assert(T::constructed == 1);
  78. assert(T::value_constructed == 1);
  79. assert(T::assigned == 0);
  80. assert(T::destroyed == 0);
  81. assert(static_cast<bool>(other) == true);
  82. assert(*other == 42);
  83. assert(static_cast<bool>(opt) == true);
  84. assert(*opt == T(42));
  85. }
  86. assert(T::alive == 0);
  87. { // non-empty to non-empty
  88. optional<T> opt(101);
  89. optional<int> other(42);
  90. T::reset_constructors();
  91. opt = std::move(other);
  92. assert(T::alive == 1);
  93. assert(T::constructed == 0);
  94. assert(T::assigned == 1);
  95. assert(T::value_assigned == 1);
  96. assert(T::destroyed == 0);
  97. assert(static_cast<bool>(other) == true);
  98. assert(*other == 42);
  99. assert(static_cast<bool>(opt) == true);
  100. assert(*opt == T(42));
  101. }
  102. assert(T::alive == 0);
  103. { // empty to non-empty
  104. optional<T> opt(101);
  105. optional<int> other;
  106. T::reset_constructors();
  107. opt = std::move(other);
  108. assert(T::alive == 0);
  109. assert(T::constructed == 0);
  110. assert(T::assigned == 0);
  111. assert(T::destroyed == 1);
  112. assert(static_cast<bool>(other) == false);
  113. assert(static_cast<bool>(opt) == false);
  114. }
  115. assert(T::alive == 0);
  116. { // empty to empty
  117. optional<T> opt;
  118. optional<int> other;
  119. T::reset_constructors();
  120. opt = std::move(other);
  121. assert(T::alive == 0);
  122. assert(T::constructed == 0);
  123. assert(T::assigned == 0);
  124. assert(T::destroyed == 0);
  125. assert(static_cast<bool>(other) == false);
  126. assert(static_cast<bool>(opt) == false);
  127. }
  128. assert(T::alive == 0);
  129. }
  130. void test_ambigious_assign() {
  131. using OptInt = std::optional<int>;
  132. {
  133. using T = AssignableFrom<OptInt&&>;
  134. T::reset();
  135. {
  136. OptInt a(42);
  137. std::optional<T> t;
  138. t = std::move(a);
  139. assert(T::type_constructed == 1);
  140. assert(T::type_assigned == 0);
  141. assert(T::int_constructed == 0);
  142. assert(T::int_assigned == 0);
  143. }
  144. {
  145. using Opt = std::optional<T>;
  146. static_assert(!std::is_assignable<Opt&, const OptInt&&>::value, "");
  147. static_assert(!std::is_assignable<Opt&, const OptInt&>::value, "");
  148. static_assert(!std::is_assignable<Opt&, OptInt&>::value, "");
  149. }
  150. }
  151. {
  152. using T = AssignableFrom<OptInt const&&>;
  153. T::reset();
  154. {
  155. const OptInt a(42);
  156. std::optional<T> t;
  157. t = std::move(a);
  158. assert(T::type_constructed == 1);
  159. assert(T::type_assigned == 0);
  160. assert(T::int_constructed == 0);
  161. assert(T::int_assigned == 0);
  162. }
  163. T::reset();
  164. {
  165. OptInt a(42);
  166. std::optional<T> t;
  167. t = std::move(a);
  168. assert(T::type_constructed == 1);
  169. assert(T::type_assigned == 0);
  170. assert(T::int_constructed == 0);
  171. assert(T::int_assigned == 0);
  172. }
  173. {
  174. using Opt = std::optional<T>;
  175. static_assert(std::is_assignable<Opt&, OptInt&&>::value, "");
  176. static_assert(!std::is_assignable<Opt&, const OptInt&>::value, "");
  177. static_assert(!std::is_assignable<Opt&, OptInt&>::value, "");
  178. }
  179. }
  180. }
  181. int main(int, char**)
  182. {
  183. test_with_test_type();
  184. test_ambigious_assign();
  185. {
  186. optional<int> opt;
  187. optional<short> opt2;
  188. opt = std::move(opt2);
  189. assert(static_cast<bool>(opt2) == false);
  190. assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
  191. }
  192. {
  193. optional<int> opt;
  194. optional<short> opt2(short{2});
  195. opt = std::move(opt2);
  196. assert(static_cast<bool>(opt2) == true);
  197. assert(*opt2 == 2);
  198. assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
  199. assert(*opt == *opt2);
  200. }
  201. {
  202. optional<int> opt(3);
  203. optional<short> opt2;
  204. opt = std::move(opt2);
  205. assert(static_cast<bool>(opt2) == false);
  206. assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
  207. }
  208. {
  209. optional<int> opt(3);
  210. optional<short> opt2(short{2});
  211. opt = std::move(opt2);
  212. assert(static_cast<bool>(opt2) == true);
  213. assert(*opt2 == 2);
  214. assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
  215. assert(*opt == *opt2);
  216. }
  217. {
  218. optional<std::unique_ptr<B>> opt;
  219. optional<std::unique_ptr<D>> other(new D());
  220. opt = std::move(other);
  221. assert(static_cast<bool>(opt) == true);
  222. assert(static_cast<bool>(other) == true);
  223. assert(opt->get() != nullptr);
  224. assert(other->get() == nullptr);
  225. }
  226. #ifndef TEST_HAS_NO_EXCEPTIONS
  227. {
  228. optional<X> opt;
  229. optional<int> opt2(42);
  230. assert(static_cast<bool>(opt2) == true);
  231. try
  232. {
  233. X::throw_now = true;
  234. opt = std::move(opt2);
  235. assert(false);
  236. }
  237. catch (int i)
  238. {
  239. assert(i == 6);
  240. assert(static_cast<bool>(opt) == false);
  241. }
  242. }
  243. #endif
  244. return 0;
  245. }