copy.pass.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // XFAIL: with_system_cxx_lib=macosx10.12
  12. // XFAIL: with_system_cxx_lib=macosx10.11
  13. // XFAIL: with_system_cxx_lib=macosx10.10
  14. // XFAIL: with_system_cxx_lib=macosx10.9
  15. // XFAIL: with_system_cxx_lib=macosx10.7
  16. // XFAIL: with_system_cxx_lib=macosx10.8
  17. // <variant>
  18. // template <class ...Types> class variant;
  19. // variant(variant const&);
  20. #include <cassert>
  21. #include <type_traits>
  22. #include <variant>
  23. #include "test_macros.h"
  24. #include "test_workarounds.h"
  25. struct NonT {
  26. NonT(int v) : value(v) {}
  27. NonT(const NonT &o) : value(o.value) {}
  28. int value;
  29. };
  30. static_assert(!std::is_trivially_copy_constructible<NonT>::value, "");
  31. struct NoCopy {
  32. NoCopy(const NoCopy &) = delete;
  33. };
  34. struct MoveOnly {
  35. MoveOnly(const MoveOnly &) = delete;
  36. MoveOnly(MoveOnly &&) = default;
  37. };
  38. struct MoveOnlyNT {
  39. MoveOnlyNT(const MoveOnlyNT &) = delete;
  40. MoveOnlyNT(MoveOnlyNT &&) {}
  41. };
  42. struct NTCopy {
  43. constexpr NTCopy(int v) : value(v) {}
  44. NTCopy(const NTCopy &that) : value(that.value) {}
  45. NTCopy(NTCopy &&) = delete;
  46. int value;
  47. };
  48. static_assert(!std::is_trivially_copy_constructible<NTCopy>::value, "");
  49. static_assert(std::is_copy_constructible<NTCopy>::value, "");
  50. struct TCopy {
  51. constexpr TCopy(int v) : value(v) {}
  52. TCopy(TCopy const &) = default;
  53. TCopy(TCopy &&) = delete;
  54. int value;
  55. };
  56. static_assert(std::is_trivially_copy_constructible<TCopy>::value, "");
  57. struct TCopyNTMove {
  58. constexpr TCopyNTMove(int v) : value(v) {}
  59. TCopyNTMove(const TCopyNTMove&) = default;
  60. TCopyNTMove(TCopyNTMove&& that) : value(that.value) { that.value = -1; }
  61. int value;
  62. };
  63. static_assert(std::is_trivially_copy_constructible<TCopyNTMove>::value, "");
  64. #ifndef TEST_HAS_NO_EXCEPTIONS
  65. struct MakeEmptyT {
  66. static int alive;
  67. MakeEmptyT() { ++alive; }
  68. MakeEmptyT(const MakeEmptyT &) {
  69. ++alive;
  70. // Don't throw from the copy constructor since variant's assignment
  71. // operator performs a copy before committing to the assignment.
  72. }
  73. MakeEmptyT(MakeEmptyT &&) { throw 42; }
  74. MakeEmptyT &operator=(const MakeEmptyT &) { throw 42; }
  75. MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
  76. ~MakeEmptyT() { --alive; }
  77. };
  78. int MakeEmptyT::alive = 0;
  79. template <class Variant> void makeEmpty(Variant &v) {
  80. Variant v2(std::in_place_type<MakeEmptyT>);
  81. try {
  82. v = std::move(v2);
  83. assert(false);
  84. } catch (...) {
  85. assert(v.valueless_by_exception());
  86. }
  87. }
  88. #endif // TEST_HAS_NO_EXCEPTIONS
  89. void test_copy_ctor_sfinae() {
  90. {
  91. using V = std::variant<int, long>;
  92. static_assert(std::is_copy_constructible<V>::value, "");
  93. }
  94. {
  95. using V = std::variant<int, NoCopy>;
  96. static_assert(!std::is_copy_constructible<V>::value, "");
  97. }
  98. {
  99. using V = std::variant<int, MoveOnly>;
  100. static_assert(!std::is_copy_constructible<V>::value, "");
  101. }
  102. {
  103. using V = std::variant<int, MoveOnlyNT>;
  104. static_assert(!std::is_copy_constructible<V>::value, "");
  105. }
  106. // The following tests are for not-yet-standardized behavior (P0602):
  107. {
  108. using V = std::variant<int, long>;
  109. static_assert(std::is_trivially_copy_constructible<V>::value, "");
  110. }
  111. {
  112. using V = std::variant<int, NTCopy>;
  113. static_assert(!std::is_trivially_copy_constructible<V>::value, "");
  114. static_assert(std::is_copy_constructible<V>::value, "");
  115. }
  116. {
  117. using V = std::variant<int, TCopy>;
  118. static_assert(std::is_trivially_copy_constructible<V>::value, "");
  119. }
  120. {
  121. using V = std::variant<int, TCopyNTMove>;
  122. static_assert(std::is_trivially_copy_constructible<V>::value, "");
  123. }
  124. }
  125. void test_copy_ctor_basic() {
  126. {
  127. std::variant<int> v(std::in_place_index<0>, 42);
  128. std::variant<int> v2 = v;
  129. assert(v2.index() == 0);
  130. assert(std::get<0>(v2) == 42);
  131. }
  132. {
  133. std::variant<int, long> v(std::in_place_index<1>, 42);
  134. std::variant<int, long> v2 = v;
  135. assert(v2.index() == 1);
  136. assert(std::get<1>(v2) == 42);
  137. }
  138. {
  139. std::variant<NonT> v(std::in_place_index<0>, 42);
  140. assert(v.index() == 0);
  141. std::variant<NonT> v2(v);
  142. assert(v2.index() == 0);
  143. assert(std::get<0>(v2).value == 42);
  144. }
  145. {
  146. std::variant<int, NonT> v(std::in_place_index<1>, 42);
  147. assert(v.index() == 1);
  148. std::variant<int, NonT> v2(v);
  149. assert(v2.index() == 1);
  150. assert(std::get<1>(v2).value == 42);
  151. }
  152. // The following tests are for not-yet-standardized behavior (P0602):
  153. {
  154. constexpr std::variant<int> v(std::in_place_index<0>, 42);
  155. static_assert(v.index() == 0, "");
  156. constexpr std::variant<int> v2 = v;
  157. static_assert(v2.index() == 0, "");
  158. static_assert(std::get<0>(v2) == 42, "");
  159. }
  160. {
  161. constexpr std::variant<int, long> v(std::in_place_index<1>, 42);
  162. static_assert(v.index() == 1, "");
  163. constexpr std::variant<int, long> v2 = v;
  164. static_assert(v2.index() == 1, "");
  165. static_assert(std::get<1>(v2) == 42, "");
  166. }
  167. {
  168. constexpr std::variant<TCopy> v(std::in_place_index<0>, 42);
  169. static_assert(v.index() == 0, "");
  170. constexpr std::variant<TCopy> v2(v);
  171. static_assert(v2.index() == 0, "");
  172. static_assert(std::get<0>(v2).value == 42, "");
  173. }
  174. {
  175. constexpr std::variant<int, TCopy> v(std::in_place_index<1>, 42);
  176. static_assert(v.index() == 1, "");
  177. constexpr std::variant<int, TCopy> v2(v);
  178. static_assert(v2.index() == 1, "");
  179. static_assert(std::get<1>(v2).value == 42, "");
  180. }
  181. {
  182. constexpr std::variant<TCopyNTMove> v(std::in_place_index<0>, 42);
  183. static_assert(v.index() == 0, "");
  184. constexpr std::variant<TCopyNTMove> v2(v);
  185. static_assert(v2.index() == 0, "");
  186. static_assert(std::get<0>(v2).value == 42, "");
  187. }
  188. {
  189. constexpr std::variant<int, TCopyNTMove> v(std::in_place_index<1>, 42);
  190. static_assert(v.index() == 1, "");
  191. constexpr std::variant<int, TCopyNTMove> v2(v);
  192. static_assert(v2.index() == 1, "");
  193. static_assert(std::get<1>(v2).value == 42, "");
  194. }
  195. }
  196. void test_copy_ctor_valueless_by_exception() {
  197. #ifndef TEST_HAS_NO_EXCEPTIONS
  198. using V = std::variant<int, MakeEmptyT>;
  199. V v1;
  200. makeEmpty(v1);
  201. const V &cv1 = v1;
  202. V v(cv1);
  203. assert(v.valueless_by_exception());
  204. #endif // TEST_HAS_NO_EXCEPTIONS
  205. }
  206. template <size_t Idx>
  207. constexpr bool test_constexpr_copy_ctor_extension_imp(
  208. std::variant<long, void*, const int> const& v)
  209. {
  210. auto v2 = v;
  211. return v2.index() == v.index() &&
  212. v2.index() == Idx &&
  213. std::get<Idx>(v2) == std::get<Idx>(v);
  214. }
  215. void test_constexpr_copy_ctor_extension() {
  216. // NOTE: This test is for not yet standardized behavior. (P0602)
  217. using V = std::variant<long, void*, const int>;
  218. #ifdef TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  219. static_assert(std::is_trivially_destructible<V>::value, "");
  220. static_assert(std::is_trivially_copy_constructible<V>::value, "");
  221. static_assert(std::is_trivially_move_constructible<V>::value, "");
  222. static_assert(!std::is_copy_assignable<V>::value, "");
  223. static_assert(!std::is_move_assignable<V>::value, "");
  224. #else // TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  225. static_assert(std::is_trivially_copyable<V>::value, "");
  226. #endif // TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  227. static_assert(test_constexpr_copy_ctor_extension_imp<0>(V(42l)), "");
  228. static_assert(test_constexpr_copy_ctor_extension_imp<1>(V(nullptr)), "");
  229. static_assert(test_constexpr_copy_ctor_extension_imp<2>(V(101)), "");
  230. }
  231. int main() {
  232. test_copy_ctor_basic();
  233. test_copy_ctor_valueless_by_exception();
  234. test_copy_ctor_sfinae();
  235. test_constexpr_copy_ctor_extension();
  236. }