T.pass.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // XFAIL: dylib-has-no-bad_variant_access && !libcpp-no-exceptions
  11. // <variant>
  12. // template <class ...Types> class variant;
  13. // template <class T>
  14. // variant& operator=(T&&) noexcept(see below);
  15. #include <cassert>
  16. #include <string>
  17. #include <type_traits>
  18. #include <variant>
  19. #include <memory>
  20. #include "test_macros.h"
  21. #include "variant_test_helpers.h"
  22. namespace MetaHelpers {
  23. struct Dummy {
  24. Dummy() = default;
  25. };
  26. struct ThrowsCtorT {
  27. ThrowsCtorT(int) noexcept(false) {}
  28. ThrowsCtorT &operator=(int) noexcept { return *this; }
  29. };
  30. struct ThrowsAssignT {
  31. ThrowsAssignT(int) noexcept {}
  32. ThrowsAssignT &operator=(int) noexcept(false) { return *this; }
  33. };
  34. struct NoThrowT {
  35. NoThrowT(int) noexcept {}
  36. NoThrowT &operator=(int) noexcept { return *this; }
  37. };
  38. } // namespace MetaHelpers
  39. namespace RuntimeHelpers {
  40. #ifndef TEST_HAS_NO_EXCEPTIONS
  41. struct ThrowsCtorT {
  42. int value;
  43. ThrowsCtorT() : value(0) {}
  44. ThrowsCtorT(int) noexcept(false) { throw 42; }
  45. ThrowsCtorT &operator=(int v) noexcept {
  46. value = v;
  47. return *this;
  48. }
  49. };
  50. struct MoveCrashes {
  51. int value;
  52. MoveCrashes(int v = 0) noexcept : value{v} {}
  53. MoveCrashes(MoveCrashes &&) noexcept { assert(false); }
  54. MoveCrashes &operator=(MoveCrashes &&) noexcept { assert(false); return *this; }
  55. MoveCrashes &operator=(int v) noexcept {
  56. value = v;
  57. return *this;
  58. }
  59. };
  60. struct ThrowsCtorTandMove {
  61. int value;
  62. ThrowsCtorTandMove() : value(0) {}
  63. ThrowsCtorTandMove(int) noexcept(false) { throw 42; }
  64. ThrowsCtorTandMove(ThrowsCtorTandMove &&) noexcept(false) { assert(false); }
  65. ThrowsCtorTandMove &operator=(int v) noexcept {
  66. value = v;
  67. return *this;
  68. }
  69. };
  70. struct ThrowsAssignT {
  71. int value;
  72. ThrowsAssignT() : value(0) {}
  73. ThrowsAssignT(int v) noexcept : value(v) {}
  74. ThrowsAssignT &operator=(int) noexcept(false) { throw 42; }
  75. };
  76. struct NoThrowT {
  77. int value;
  78. NoThrowT() : value(0) {}
  79. NoThrowT(int v) noexcept : value(v) {}
  80. NoThrowT &operator=(int v) noexcept {
  81. value = v;
  82. return *this;
  83. }
  84. };
  85. #endif // !defined(TEST_HAS_NO_EXCEPTIONS)
  86. } // namespace RuntimeHelpers
  87. void test_T_assignment_noexcept() {
  88. using namespace MetaHelpers;
  89. {
  90. using V = std::variant<Dummy, NoThrowT>;
  91. static_assert(std::is_nothrow_assignable<V, int>::value, "");
  92. }
  93. {
  94. using V = std::variant<Dummy, ThrowsCtorT>;
  95. static_assert(!std::is_nothrow_assignable<V, int>::value, "");
  96. }
  97. {
  98. using V = std::variant<Dummy, ThrowsAssignT>;
  99. static_assert(!std::is_nothrow_assignable<V, int>::value, "");
  100. }
  101. }
  102. void test_T_assignment_sfinae() {
  103. {
  104. using V = std::variant<long, long long>;
  105. static_assert(!std::is_assignable<V, int>::value, "ambiguous");
  106. }
  107. {
  108. using V = std::variant<std::string, std::string>;
  109. static_assert(!std::is_assignable<V, const char *>::value, "ambiguous");
  110. }
  111. {
  112. using V = std::variant<std::string, void *>;
  113. static_assert(!std::is_assignable<V, int>::value, "no matching operator=");
  114. }
  115. {
  116. using V = std::variant<std::string, float>;
  117. static_assert(std::is_assignable<V, int>::value == VariantAllowsNarrowingConversions,
  118. "no matching operator=");
  119. }
  120. {
  121. using V = std::variant<std::unique_ptr<int>, bool>;
  122. static_assert(!std::is_assignable<V, std::unique_ptr<char>>::value,
  123. "no explicit bool in operator=");
  124. struct X {
  125. operator void*();
  126. };
  127. static_assert(!std::is_assignable<V, X>::value,
  128. "no boolean conversion in operator=");
  129. static_assert(!std::is_assignable<V, std::false_type>::value,
  130. "no converted to bool in operator=");
  131. }
  132. {
  133. struct X {};
  134. struct Y {
  135. operator X();
  136. };
  137. using V = std::variant<X>;
  138. static_assert(std::is_assignable<V, Y>::value,
  139. "regression on user-defined conversions in operator=");
  140. }
  141. #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
  142. {
  143. using V = std::variant<int, int &&>;
  144. static_assert(!std::is_assignable<V, int>::value, "ambiguous");
  145. }
  146. {
  147. using V = std::variant<int, const int &>;
  148. static_assert(!std::is_assignable<V, int>::value, "ambiguous");
  149. }
  150. #endif // TEST_VARIANT_HAS_NO_REFERENCES
  151. }
  152. void test_T_assignment_basic() {
  153. {
  154. std::variant<int> v(43);
  155. v = 42;
  156. assert(v.index() == 0);
  157. assert(std::get<0>(v) == 42);
  158. }
  159. {
  160. std::variant<int, long> v(43l);
  161. v = 42;
  162. assert(v.index() == 0);
  163. assert(std::get<0>(v) == 42);
  164. v = 43l;
  165. assert(v.index() == 1);
  166. assert(std::get<1>(v) == 43);
  167. }
  168. #ifndef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
  169. {
  170. std::variant<unsigned, long> v;
  171. v = 42;
  172. assert(v.index() == 1);
  173. assert(std::get<1>(v) == 42);
  174. v = 43u;
  175. assert(v.index() == 0);
  176. assert(std::get<0>(v) == 43);
  177. }
  178. #endif
  179. {
  180. std::variant<std::string, bool> v = true;
  181. v = "bar";
  182. assert(v.index() == 0);
  183. assert(std::get<0>(v) == "bar");
  184. }
  185. {
  186. std::variant<bool, std::unique_ptr<int>> v;
  187. v = nullptr;
  188. assert(v.index() == 1);
  189. assert(std::get<1>(v) == nullptr);
  190. }
  191. {
  192. std::variant<bool volatile, int> v = 42;
  193. v = false;
  194. assert(v.index() == 0);
  195. assert(!std::get<0>(v));
  196. bool lvt = true;
  197. v = lvt;
  198. assert(v.index() == 0);
  199. assert(std::get<0>(v));
  200. }
  201. #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
  202. {
  203. using V = std::variant<int &, int &&, long>;
  204. int x = 42;
  205. V v(43l);
  206. v = x;
  207. assert(v.index() == 0);
  208. assert(&std::get<0>(v) == &x);
  209. v = std::move(x);
  210. assert(v.index() == 1);
  211. assert(&std::get<1>(v) == &x);
  212. // 'long' is selected by FUN(const int &) since 'const int &' cannot bind
  213. // to 'int&'.
  214. const int &cx = x;
  215. v = cx;
  216. assert(v.index() == 2);
  217. assert(std::get<2>(v) == 42);
  218. }
  219. #endif // TEST_VARIANT_HAS_NO_REFERENCES
  220. }
  221. void test_T_assignment_performs_construction() {
  222. using namespace RuntimeHelpers;
  223. #ifndef TEST_HAS_NO_EXCEPTIONS
  224. {
  225. using V = std::variant<std::string, ThrowsCtorT>;
  226. V v(std::in_place_type<std::string>, "hello");
  227. try {
  228. v = 42;
  229. assert(false);
  230. } catch (...) { /* ... */
  231. }
  232. assert(v.index() == 0);
  233. assert(std::get<0>(v) == "hello");
  234. }
  235. {
  236. using V = std::variant<ThrowsAssignT, std::string>;
  237. V v(std::in_place_type<std::string>, "hello");
  238. v = 42;
  239. assert(v.index() == 0);
  240. assert(std::get<0>(v).value == 42);
  241. }
  242. #endif // TEST_HAS_NO_EXCEPTIONS
  243. }
  244. void test_T_assignment_performs_assignment() {
  245. using namespace RuntimeHelpers;
  246. #ifndef TEST_HAS_NO_EXCEPTIONS
  247. {
  248. using V = std::variant<ThrowsCtorT>;
  249. V v;
  250. v = 42;
  251. assert(v.index() == 0);
  252. assert(std::get<0>(v).value == 42);
  253. }
  254. {
  255. using V = std::variant<ThrowsCtorT, std::string>;
  256. V v;
  257. v = 42;
  258. assert(v.index() == 0);
  259. assert(std::get<0>(v).value == 42);
  260. }
  261. {
  262. using V = std::variant<ThrowsAssignT>;
  263. V v(100);
  264. try {
  265. v = 42;
  266. assert(false);
  267. } catch (...) { /* ... */
  268. }
  269. assert(v.index() == 0);
  270. assert(std::get<0>(v).value == 100);
  271. }
  272. {
  273. using V = std::variant<std::string, ThrowsAssignT>;
  274. V v(100);
  275. try {
  276. v = 42;
  277. assert(false);
  278. } catch (...) { /* ... */
  279. }
  280. assert(v.index() == 1);
  281. assert(std::get<1>(v).value == 100);
  282. }
  283. #endif // TEST_HAS_NO_EXCEPTIONS
  284. }
  285. int main(int, char**) {
  286. test_T_assignment_basic();
  287. test_T_assignment_performs_construction();
  288. test_T_assignment_performs_assignment();
  289. test_T_assignment_noexcept();
  290. test_T_assignment_sfinae();
  291. return 0;
  292. }