copy.pass.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // variant(variant const&);
  14. #include <cassert>
  15. #include <type_traits>
  16. #include <variant>
  17. #include "test_macros.h"
  18. #include "test_workarounds.h"
  19. struct NonT {
  20. NonT(int v) : value(v) {}
  21. NonT(const NonT &o) : value(o.value) {}
  22. int value;
  23. };
  24. static_assert(!std::is_trivially_copy_constructible<NonT>::value, "");
  25. struct NoCopy {
  26. NoCopy(const NoCopy &) = delete;
  27. };
  28. struct MoveOnly {
  29. MoveOnly(const MoveOnly &) = delete;
  30. MoveOnly(MoveOnly &&) = default;
  31. };
  32. struct MoveOnlyNT {
  33. MoveOnlyNT(const MoveOnlyNT &) = delete;
  34. MoveOnlyNT(MoveOnlyNT &&) {}
  35. };
  36. #ifndef TEST_HAS_NO_EXCEPTIONS
  37. struct MakeEmptyT {
  38. static int alive;
  39. MakeEmptyT() { ++alive; }
  40. MakeEmptyT(const MakeEmptyT &) {
  41. ++alive;
  42. // Don't throw from the copy constructor since variant's assignment
  43. // operator performs a copy before committing to the assignment.
  44. }
  45. MakeEmptyT(MakeEmptyT &&) { throw 42; }
  46. MakeEmptyT &operator=(const MakeEmptyT &) { throw 42; }
  47. MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
  48. ~MakeEmptyT() { --alive; }
  49. };
  50. int MakeEmptyT::alive = 0;
  51. template <class Variant> void makeEmpty(Variant &v) {
  52. Variant v2(std::in_place_type<MakeEmptyT>);
  53. try {
  54. v = v2;
  55. assert(false);
  56. } catch (...) {
  57. assert(v.valueless_by_exception());
  58. }
  59. }
  60. #endif // TEST_HAS_NO_EXCEPTIONS
  61. void test_copy_ctor_sfinae() {
  62. {
  63. using V = std::variant<int, long>;
  64. static_assert(std::is_copy_constructible<V>::value, "");
  65. }
  66. {
  67. using V = std::variant<int, NoCopy>;
  68. static_assert(!std::is_copy_constructible<V>::value, "");
  69. }
  70. {
  71. using V = std::variant<int, MoveOnly>;
  72. static_assert(!std::is_copy_constructible<V>::value, "");
  73. }
  74. {
  75. using V = std::variant<int, MoveOnlyNT>;
  76. static_assert(!std::is_copy_constructible<V>::value, "");
  77. }
  78. }
  79. void test_copy_ctor_basic() {
  80. {
  81. std::variant<int> v(std::in_place_index<0>, 42);
  82. std::variant<int> v2 = v;
  83. assert(v2.index() == 0);
  84. assert(std::get<0>(v2) == 42);
  85. }
  86. {
  87. std::variant<int, long> v(std::in_place_index<1>, 42);
  88. std::variant<int, long> v2 = v;
  89. assert(v2.index() == 1);
  90. assert(std::get<1>(v2) == 42);
  91. }
  92. {
  93. std::variant<NonT> v(std::in_place_index<0>, 42);
  94. assert(v.index() == 0);
  95. std::variant<NonT> v2(v);
  96. assert(v2.index() == 0);
  97. assert(std::get<0>(v2).value == 42);
  98. }
  99. {
  100. std::variant<int, NonT> v(std::in_place_index<1>, 42);
  101. assert(v.index() == 1);
  102. std::variant<int, NonT> v2(v);
  103. assert(v2.index() == 1);
  104. assert(std::get<1>(v2).value == 42);
  105. }
  106. }
  107. void test_copy_ctor_valueless_by_exception() {
  108. #ifndef TEST_HAS_NO_EXCEPTIONS
  109. using V = std::variant<int, MakeEmptyT>;
  110. V v1;
  111. makeEmpty(v1);
  112. const V &cv1 = v1;
  113. V v(cv1);
  114. assert(v.valueless_by_exception());
  115. #endif
  116. }
  117. template <size_t Idx>
  118. constexpr bool test_constexpr_copy_ctor_extension_imp(
  119. std::variant<long, void*, const int> const& v)
  120. {
  121. auto v2 = v;
  122. return v2.index() == v.index() &&
  123. v2.index() == Idx &&
  124. std::get<Idx>(v2) == std::get<Idx>(v);
  125. }
  126. void test_constexpr_copy_ctor_extension() {
  127. #if defined(_LIBCPP_VER) || defined(_MSVC_STL_VER)
  128. using V = std::variant<long, void*, const int>;
  129. #ifdef TEST_WORKAROUND_C1XX_BROKEN_IS_TRIVIALLY_COPYABLE
  130. static_assert(std::is_trivially_destructible<V>::value, "");
  131. static_assert(std::is_trivially_copy_constructible<V>::value, "");
  132. static_assert(std::is_trivially_move_constructible<V>::value, "");
  133. static_assert(!std::is_copy_assignable<V>::value, "");
  134. static_assert(!std::is_move_assignable<V>::value, "");
  135. #else
  136. static_assert(std::is_trivially_copyable<V>::value, "");
  137. #endif
  138. static_assert(test_constexpr_copy_ctor_extension_imp<0>(V(42l)), "");
  139. static_assert(test_constexpr_copy_ctor_extension_imp<1>(V(nullptr)), "");
  140. static_assert(test_constexpr_copy_ctor_extension_imp<2>(V(101)), "");
  141. #endif
  142. }
  143. int main() {
  144. test_copy_ctor_basic();
  145. test_copy_ctor_valueless_by_exception();
  146. test_copy_ctor_sfinae();
  147. test_constexpr_copy_ctor_extension();
  148. }