convert_copy.pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <tuple>
  9. // template <class... Types> class tuple;
  10. // template <class... UTypes> tuple(const tuple<UTypes...>& u);
  11. // UNSUPPORTED: c++98, c++03
  12. #include <tuple>
  13. #include <utility>
  14. #include <string>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. struct Explicit {
  18. int value;
  19. explicit Explicit(int x) : value(x) {}
  20. };
  21. struct Implicit {
  22. int value;
  23. Implicit(int x) : value(x) {}
  24. };
  25. struct ExplicitTwo {
  26. ExplicitTwo() {}
  27. ExplicitTwo(ExplicitTwo const&) {}
  28. ExplicitTwo(ExplicitTwo &&) {}
  29. template <class T, class = typename std::enable_if<!std::is_same<T, ExplicitTwo>::value>::type>
  30. explicit ExplicitTwo(T) {}
  31. };
  32. struct B
  33. {
  34. int id_;
  35. explicit B(int i) : id_(i) {}
  36. };
  37. struct D
  38. : B
  39. {
  40. explicit D(int i) : B(i) {}
  41. };
  42. #if TEST_STD_VER > 11
  43. struct A
  44. {
  45. int id_;
  46. constexpr A(int i) : id_(i) {}
  47. friend constexpr bool operator==(const A& x, const A& y) {return x.id_ == y.id_;}
  48. };
  49. struct C
  50. {
  51. int id_;
  52. constexpr explicit C(int i) : id_(i) {}
  53. friend constexpr bool operator==(const C& x, const C& y) {return x.id_ == y.id_;}
  54. };
  55. #endif
  56. int main(int, char**)
  57. {
  58. {
  59. typedef std::tuple<long> T0;
  60. typedef std::tuple<long long> T1;
  61. T0 t0(2);
  62. T1 t1 = t0;
  63. assert(std::get<0>(t1) == 2);
  64. }
  65. #if TEST_STD_VER > 11
  66. {
  67. typedef std::tuple<int> T0;
  68. typedef std::tuple<A> T1;
  69. constexpr T0 t0(2);
  70. constexpr T1 t1 = t0;
  71. static_assert(std::get<0>(t1) == 2, "");
  72. }
  73. {
  74. typedef std::tuple<int> T0;
  75. typedef std::tuple<C> T1;
  76. constexpr T0 t0(2);
  77. constexpr T1 t1{t0};
  78. static_assert(std::get<0>(t1) == C(2), "");
  79. }
  80. #endif
  81. {
  82. typedef std::tuple<long, char> T0;
  83. typedef std::tuple<long long, int> T1;
  84. T0 t0(2, 'a');
  85. T1 t1 = t0;
  86. assert(std::get<0>(t1) == 2);
  87. assert(std::get<1>(t1) == int('a'));
  88. }
  89. {
  90. typedef std::tuple<long, char, D> T0;
  91. typedef std::tuple<long long, int, B> T1;
  92. T0 t0(2, 'a', D(3));
  93. T1 t1 = t0;
  94. assert(std::get<0>(t1) == 2);
  95. assert(std::get<1>(t1) == int('a'));
  96. assert(std::get<2>(t1).id_ == 3);
  97. }
  98. {
  99. D d(3);
  100. typedef std::tuple<long, char, D&> T0;
  101. typedef std::tuple<long long, int, B&> T1;
  102. T0 t0(2, 'a', d);
  103. T1 t1 = t0;
  104. d.id_ = 2;
  105. assert(std::get<0>(t1) == 2);
  106. assert(std::get<1>(t1) == int('a'));
  107. assert(std::get<2>(t1).id_ == 2);
  108. }
  109. {
  110. typedef std::tuple<long, char, int> T0;
  111. typedef std::tuple<long long, int, B> T1;
  112. T0 t0(2, 'a', 3);
  113. T1 t1(t0);
  114. assert(std::get<0>(t1) == 2);
  115. assert(std::get<1>(t1) == int('a'));
  116. assert(std::get<2>(t1).id_ == 3);
  117. }
  118. {
  119. const std::tuple<int> t1(42);
  120. std::tuple<Explicit> t2(t1);
  121. assert(std::get<0>(t2).value == 42);
  122. }
  123. {
  124. const std::tuple<int> t1(42);
  125. std::tuple<Implicit> t2 = t1;
  126. assert(std::get<0>(t2).value == 42);
  127. }
  128. {
  129. static_assert(std::is_convertible<ExplicitTwo&&, ExplicitTwo>::value, "");
  130. static_assert(std::is_convertible<std::tuple<ExplicitTwo&&>&&, const std::tuple<ExplicitTwo>&>::value, "");
  131. ExplicitTwo e;
  132. std::tuple<ExplicitTwo> t = std::tuple<ExplicitTwo&&>(std::move(e));
  133. ((void)t);
  134. }
  135. return 0;
  136. }