copy.pass.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. struct NonT {
  19. NonT(int v) : value(v) {}
  20. NonT(NonT const &o) : value(o.value) {}
  21. int value;
  22. };
  23. static_assert(!std::is_trivially_copy_constructible<NonT>::value, "");
  24. struct NoCopy {
  25. NoCopy(NoCopy const &) = delete;
  26. };
  27. struct MoveOnly {
  28. MoveOnly(MoveOnly const &) = delete;
  29. MoveOnly(MoveOnly &&) = default;
  30. };
  31. struct MoveOnlyNT {
  32. MoveOnlyNT(MoveOnlyNT const &) = delete;
  33. MoveOnlyNT(MoveOnlyNT &&) {}
  34. };
  35. #ifndef TEST_HAS_NO_EXCEPTIONS
  36. struct MakeEmptyT {
  37. static int alive;
  38. MakeEmptyT() { ++alive; }
  39. MakeEmptyT(MakeEmptyT const &) {
  40. ++alive;
  41. // Don't throw from the copy constructor since variant's assignment
  42. // operator performs a copy before committing to the assignment.
  43. }
  44. MakeEmptyT(MakeEmptyT &&) { throw 42; }
  45. MakeEmptyT &operator=(MakeEmptyT const &) { throw 42; }
  46. MakeEmptyT &operator=(MakeEmptyT &&) { throw 42; }
  47. ~MakeEmptyT() { --alive; }
  48. };
  49. int MakeEmptyT::alive = 0;
  50. template <class Variant> void makeEmpty(Variant &v) {
  51. Variant v2(std::in_place_type<MakeEmptyT>);
  52. try {
  53. v = v2;
  54. assert(false);
  55. } catch (...) {
  56. assert(v.valueless_by_exception());
  57. }
  58. }
  59. #endif // TEST_HAS_NO_EXCEPTIONS
  60. void test_copy_ctor_sfinae() {
  61. {
  62. using V = std::variant<int, long>;
  63. static_assert(std::is_copy_constructible<V>::value, "");
  64. }
  65. {
  66. using V = std::variant<int, NoCopy>;
  67. static_assert(!std::is_copy_constructible<V>::value, "");
  68. }
  69. {
  70. using V = std::variant<int, MoveOnly>;
  71. static_assert(!std::is_copy_constructible<V>::value, "");
  72. }
  73. {
  74. using V = std::variant<int, MoveOnlyNT>;
  75. static_assert(!std::is_copy_constructible<V>::value, "");
  76. }
  77. }
  78. void test_copy_ctor_basic() {
  79. {
  80. std::variant<int> v(std::in_place_index<0>, 42);
  81. std::variant<int> v2 = v;
  82. assert(v2.index() == 0);
  83. assert(std::get<0>(v2) == 42);
  84. }
  85. {
  86. std::variant<int, long> v(std::in_place_index<1>, 42);
  87. std::variant<int, long> v2 = v;
  88. assert(v2.index() == 1);
  89. assert(std::get<1>(v2) == 42);
  90. }
  91. {
  92. std::variant<NonT> v(std::in_place_index<0>, 42);
  93. assert(v.index() == 0);
  94. std::variant<NonT> v2(v);
  95. assert(v2.index() == 0);
  96. assert(std::get<0>(v2).value == 42);
  97. }
  98. {
  99. std::variant<int, NonT> v(std::in_place_index<1>, 42);
  100. assert(v.index() == 1);
  101. std::variant<int, NonT> v2(v);
  102. assert(v2.index() == 1);
  103. assert(std::get<1>(v2).value == 42);
  104. }
  105. }
  106. void test_copy_ctor_valueless_by_exception() {
  107. #ifndef TEST_HAS_NO_EXCEPTIONS
  108. using V = std::variant<int, MakeEmptyT>;
  109. V v1;
  110. makeEmpty(v1);
  111. V const &cv1 = v1;
  112. V v(cv1);
  113. assert(v.valueless_by_exception());
  114. #endif
  115. }
  116. int main() {
  117. test_copy_ctor_basic();
  118. test_copy_ctor_valueless_by_exception();
  119. test_copy_ctor_sfinae();
  120. }