const_optional_U.pass.cpp 6.7 KB

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