copy.pass.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #ifndef TEST_HAS_NO_EXCEPTIONS
  43. struct MakeEmptyT {
  44. static int alive;
  45. MakeEmptyT() { ++alive; }
  46. MakeEmptyT(const MakeEmptyT &) {
  47. ++alive;
  48. // Don't throw from the copy constructor since variant's assignment
  49. // operator performs a copy before committing to the assignment.
  50. }
  51. MakeEmptyT(MakeEmptyT &&) { throw 42; }
  52. MakeEmptyT &operator=(const MakeEmptyT &) { throw 42; }
  53. MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
  54. ~MakeEmptyT() { --alive; }
  55. };
  56. int MakeEmptyT::alive = 0;
  57. template <class Variant> void makeEmpty(Variant &v) {
  58. Variant v2(std::in_place_type<MakeEmptyT>);
  59. try {
  60. v = v2;
  61. assert(false);
  62. } catch (...) {
  63. assert(v.valueless_by_exception());
  64. }
  65. }
  66. #endif // TEST_HAS_NO_EXCEPTIONS
  67. void test_copy_ctor_sfinae() {
  68. {
  69. using V = std::variant<int, long>;
  70. static_assert(std::is_copy_constructible<V>::value, "");
  71. }
  72. {
  73. using V = std::variant<int, NoCopy>;
  74. static_assert(!std::is_copy_constructible<V>::value, "");
  75. }
  76. {
  77. using V = std::variant<int, MoveOnly>;
  78. static_assert(!std::is_copy_constructible<V>::value, "");
  79. }
  80. {
  81. using V = std::variant<int, MoveOnlyNT>;
  82. static_assert(!std::is_copy_constructible<V>::value, "");
  83. }
  84. }
  85. void test_copy_ctor_basic() {
  86. {
  87. std::variant<int> v(std::in_place_index<0>, 42);
  88. std::variant<int> v2 = v;
  89. assert(v2.index() == 0);
  90. assert(std::get<0>(v2) == 42);
  91. }
  92. {
  93. std::variant<int, long> v(std::in_place_index<1>, 42);
  94. std::variant<int, long> v2 = v;
  95. assert(v2.index() == 1);
  96. assert(std::get<1>(v2) == 42);
  97. }
  98. {
  99. std::variant<NonT> v(std::in_place_index<0>, 42);
  100. assert(v.index() == 0);
  101. std::variant<NonT> v2(v);
  102. assert(v2.index() == 0);
  103. assert(std::get<0>(v2).value == 42);
  104. }
  105. {
  106. std::variant<int, NonT> v(std::in_place_index<1>, 42);
  107. assert(v.index() == 1);
  108. std::variant<int, NonT> v2(v);
  109. assert(v2.index() == 1);
  110. assert(std::get<1>(v2).value == 42);
  111. }
  112. }
  113. void test_copy_ctor_valueless_by_exception() {
  114. #ifndef TEST_HAS_NO_EXCEPTIONS
  115. using V = std::variant<int, MakeEmptyT>;
  116. V v1;
  117. makeEmpty(v1);
  118. const V &cv1 = v1;
  119. V v(cv1);
  120. assert(v.valueless_by_exception());
  121. #endif
  122. }
  123. template <size_t Idx>
  124. constexpr bool test_constexpr_copy_ctor_extension_imp(
  125. std::variant<long, void*, const int> const& v)
  126. {
  127. auto v2 = v;
  128. return v2.index() == v.index() &&
  129. v2.index() == Idx &&
  130. std::get<Idx>(v2) == std::get<Idx>(v);
  131. }
  132. void test_constexpr_copy_ctor_extension() {
  133. // NOTE: This test is for not yet standardized behavior.
  134. using V = std::variant<long, void*, const int>;
  135. #ifdef TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  136. static_assert(std::is_trivially_destructible<V>::value, "");
  137. static_assert(std::is_trivially_copy_constructible<V>::value, "");
  138. static_assert(std::is_trivially_move_constructible<V>::value, "");
  139. static_assert(!std::is_copy_assignable<V>::value, "");
  140. static_assert(!std::is_move_assignable<V>::value, "");
  141. #else
  142. static_assert(std::is_trivially_copyable<V>::value, "");
  143. #endif
  144. static_assert(test_constexpr_copy_ctor_extension_imp<0>(V(42l)), "");
  145. static_assert(test_constexpr_copy_ctor_extension_imp<1>(V(nullptr)), "");
  146. static_assert(test_constexpr_copy_ctor_extension_imp<2>(V(101)), "");
  147. }
  148. int main() {
  149. test_copy_ctor_basic();
  150. test_copy_ctor_valueless_by_exception();
  151. test_copy_ctor_sfinae();
  152. test_constexpr_copy_ctor_extension();
  153. }