T.pass.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  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 "test_macros.h"
  20. #include "variant_test_helpers.hpp"
  21. namespace MetaHelpers {
  22. struct Dummy {
  23. Dummy() = default;
  24. };
  25. struct ThrowsCtorT {
  26. ThrowsCtorT(int) noexcept(false) {}
  27. ThrowsCtorT &operator=(int) noexcept { return *this; }
  28. };
  29. struct ThrowsAssignT {
  30. ThrowsAssignT(int) noexcept {}
  31. ThrowsAssignT &operator=(int) noexcept(false) { return *this; }
  32. };
  33. struct NoThrowT {
  34. NoThrowT(int) noexcept {}
  35. NoThrowT &operator=(int) noexcept { return *this; }
  36. };
  37. } // namespace MetaHelpers
  38. namespace RuntimeHelpers {
  39. #ifndef TEST_HAS_NO_EXCEPTIONS
  40. struct ThrowsCtorT {
  41. int value;
  42. ThrowsCtorT() : value(0) {}
  43. ThrowsCtorT(int) noexcept(false) { throw 42; }
  44. ThrowsCtorT &operator=(int v) noexcept {
  45. value = v;
  46. return *this;
  47. }
  48. };
  49. struct ThrowsAssignT {
  50. int value;
  51. ThrowsAssignT() : value(0) {}
  52. ThrowsAssignT(int v) noexcept : value(v) {}
  53. ThrowsAssignT &operator=(int) noexcept(false) { throw 42; }
  54. };
  55. struct NoThrowT {
  56. int value;
  57. NoThrowT() : value(0) {}
  58. NoThrowT(int v) noexcept : value(v) {}
  59. NoThrowT &operator=(int v) noexcept {
  60. value = v;
  61. return *this;
  62. }
  63. };
  64. #endif // !defined(TEST_HAS_NO_EXCEPTIONS)
  65. } // namespace RuntimeHelpers
  66. void test_T_assignment_noexcept() {
  67. using namespace MetaHelpers;
  68. {
  69. using V = std::variant<Dummy, NoThrowT>;
  70. static_assert(std::is_nothrow_assignable<V, int>::value, "");
  71. }
  72. {
  73. using V = std::variant<Dummy, ThrowsCtorT>;
  74. static_assert(!std::is_nothrow_assignable<V, int>::value, "");
  75. }
  76. {
  77. using V = std::variant<Dummy, ThrowsAssignT>;
  78. static_assert(!std::is_nothrow_assignable<V, int>::value, "");
  79. }
  80. }
  81. void test_T_assignment_sfinae() {
  82. {
  83. using V = std::variant<long, unsigned>;
  84. static_assert(!std::is_assignable<V, int>::value, "ambiguous");
  85. }
  86. {
  87. using V = std::variant<std::string, std::string>;
  88. static_assert(!std::is_assignable<V, const char *>::value, "ambiguous");
  89. }
  90. {
  91. using V = std::variant<std::string, void *>;
  92. static_assert(!std::is_assignable<V, int>::value, "no matching operator=");
  93. }
  94. #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
  95. {
  96. using V = std::variant<int, int &&>;
  97. static_assert(!std::is_assignable<V, int>::value, "ambiguous");
  98. }
  99. {
  100. using V = std::variant<int, int const &>;
  101. static_assert(!std::is_assignable<V, int>::value, "ambiguous");
  102. }
  103. #endif
  104. }
  105. void test_T_assignment_basic() {
  106. {
  107. std::variant<int> v(43);
  108. v = 42;
  109. assert(v.index() == 0);
  110. assert(std::get<0>(v) == 42);
  111. }
  112. {
  113. std::variant<int, long> v(43l);
  114. v = 42;
  115. assert(v.index() == 0);
  116. assert(std::get<0>(v) == 42);
  117. v = 43l;
  118. assert(v.index() == 1);
  119. assert(std::get<1>(v) == 43);
  120. }
  121. #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
  122. {
  123. using V = std::variant<int &, int &&, long>;
  124. int x = 42;
  125. V v(43l);
  126. v = x;
  127. assert(v.index() == 0);
  128. assert(&std::get<0>(v) == &x);
  129. v = std::move(x);
  130. assert(v.index() == 1);
  131. assert(&std::get<1>(v) == &x);
  132. // 'long' is selected by FUN(int const&) since 'int const&' cannot bind
  133. // to 'int&'.
  134. int const &cx = x;
  135. v = cx;
  136. assert(v.index() == 2);
  137. assert(std::get<2>(v) == 42);
  138. }
  139. #endif
  140. }
  141. void test_T_assignment_performs_construction() {
  142. using namespace RuntimeHelpers;
  143. #ifndef TEST_HAS_NO_EXCEPTIONS
  144. {
  145. using V = std::variant<std::string, ThrowsCtorT>;
  146. V v(std::in_place_type<std::string>, "hello");
  147. try {
  148. v = 42;
  149. } catch (...) { /* ... */
  150. }
  151. assert(v.valueless_by_exception());
  152. }
  153. {
  154. using V = std::variant<ThrowsAssignT, std::string>;
  155. V v(std::in_place_type<std::string>, "hello");
  156. v = 42;
  157. assert(v.index() == 0);
  158. assert(std::get<0>(v).value == 42);
  159. }
  160. #endif
  161. }
  162. void test_T_assignment_performs_assignment() {
  163. using namespace RuntimeHelpers;
  164. #ifndef TEST_HAS_NO_EXCEPTIONS
  165. {
  166. using V = std::variant<ThrowsCtorT>;
  167. V v;
  168. v = 42;
  169. assert(v.index() == 0);
  170. assert(std::get<0>(v).value == 42);
  171. }
  172. {
  173. using V = std::variant<ThrowsCtorT, std::string>;
  174. V v;
  175. v = 42;
  176. assert(v.index() == 0);
  177. assert(std::get<0>(v).value == 42);
  178. }
  179. {
  180. using V = std::variant<ThrowsAssignT>;
  181. V v(100);
  182. try {
  183. v = 42;
  184. assert(false);
  185. } catch (...) { /* ... */
  186. }
  187. assert(v.index() == 0);
  188. assert(std::get<0>(v).value == 100);
  189. }
  190. {
  191. using V = std::variant<std::string, ThrowsAssignT>;
  192. V v(100);
  193. try {
  194. v = 42;
  195. assert(false);
  196. } catch (...) { /* ... */
  197. }
  198. assert(v.index() == 1);
  199. assert(std::get<1>(v).value == 100);
  200. }
  201. #endif
  202. }
  203. int main() {
  204. test_T_assignment_basic();
  205. test_T_assignment_performs_construction();
  206. test_T_assignment_performs_assignment();
  207. test_T_assignment_noexcept();
  208. test_T_assignment_sfinae();
  209. }