convert_copy.pass.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 B
  26. {
  27. int id_;
  28. explicit B(int i) : id_(i) {}
  29. };
  30. struct D
  31. : B
  32. {
  33. explicit D(int i) : B(i) {}
  34. };
  35. #if TEST_STD_VER > 11
  36. struct A
  37. {
  38. int id_;
  39. constexpr A(int i) : id_(i) {}
  40. friend constexpr bool operator==(const A& x, const A& y) {return x.id_ == y.id_;}
  41. };
  42. struct C
  43. {
  44. int id_;
  45. constexpr explicit C(int i) : id_(i) {}
  46. friend constexpr bool operator==(const C& x, const C& y) {return x.id_ == y.id_;}
  47. };
  48. #endif
  49. int main(int, char**)
  50. {
  51. {
  52. typedef std::tuple<long> T0;
  53. typedef std::tuple<long long> T1;
  54. T0 t0(2);
  55. T1 t1 = t0;
  56. assert(std::get<0>(t1) == 2);
  57. }
  58. #if TEST_STD_VER > 11
  59. {
  60. typedef std::tuple<int> T0;
  61. typedef std::tuple<A> T1;
  62. constexpr T0 t0(2);
  63. constexpr T1 t1 = t0;
  64. static_assert(std::get<0>(t1) == 2, "");
  65. }
  66. {
  67. typedef std::tuple<int> T0;
  68. typedef std::tuple<C> T1;
  69. constexpr T0 t0(2);
  70. constexpr T1 t1{t0};
  71. static_assert(std::get<0>(t1) == C(2), "");
  72. }
  73. #endif
  74. {
  75. typedef std::tuple<long, char> T0;
  76. typedef std::tuple<long long, int> T1;
  77. T0 t0(2, 'a');
  78. T1 t1 = t0;
  79. assert(std::get<0>(t1) == 2);
  80. assert(std::get<1>(t1) == int('a'));
  81. }
  82. {
  83. typedef std::tuple<long, char, D> T0;
  84. typedef std::tuple<long long, int, B> T1;
  85. T0 t0(2, 'a', D(3));
  86. T1 t1 = t0;
  87. assert(std::get<0>(t1) == 2);
  88. assert(std::get<1>(t1) == int('a'));
  89. assert(std::get<2>(t1).id_ == 3);
  90. }
  91. {
  92. D d(3);
  93. typedef std::tuple<long, char, D&> T0;
  94. typedef std::tuple<long long, int, B&> T1;
  95. T0 t0(2, 'a', d);
  96. T1 t1 = t0;
  97. d.id_ = 2;
  98. assert(std::get<0>(t1) == 2);
  99. assert(std::get<1>(t1) == int('a'));
  100. assert(std::get<2>(t1).id_ == 2);
  101. }
  102. {
  103. typedef std::tuple<long, char, int> T0;
  104. typedef std::tuple<long long, int, B> T1;
  105. T0 t0(2, 'a', 3);
  106. T1 t1(t0);
  107. assert(std::get<0>(t1) == 2);
  108. assert(std::get<1>(t1) == int('a'));
  109. assert(std::get<2>(t1).id_ == 3);
  110. }
  111. {
  112. const std::tuple<int> t1(42);
  113. std::tuple<Explicit> t2(t1);
  114. assert(std::get<0>(t2).value == 42);
  115. }
  116. {
  117. const std::tuple<int> t1(42);
  118. std::tuple<Implicit> t2 = t1;
  119. assert(std::get<0>(t2).value == 42);
  120. }
  121. return 0;
  122. }