optional_U.pass.cpp 7.3 KB

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