convert_move.pass.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(tuple<UTypes...>&& u);
  11. // UNSUPPORTED: c++98, c++03
  12. #include <tuple>
  13. #include <string>
  14. #include <memory>
  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. virtual ~B() {}
  30. };
  31. struct D
  32. : B
  33. {
  34. explicit D(int i) : B(i) {}
  35. };
  36. struct BonkersBananas {
  37. template <class T>
  38. operator T() &&;
  39. template <class T, class = void>
  40. explicit operator T() && = delete;
  41. };
  42. void test_bonkers_bananas_conversion() {
  43. using ReturnType = std::tuple<int, int>;
  44. static_assert(std::is_convertible<BonkersBananas, ReturnType>(), "");
  45. static_assert(!std::is_constructible<ReturnType, BonkersBananas>(), "");
  46. }
  47. int main(int, char**)
  48. {
  49. {
  50. typedef std::tuple<long> T0;
  51. typedef std::tuple<long long> T1;
  52. T0 t0(2);
  53. T1 t1 = std::move(t0);
  54. assert(std::get<0>(t1) == 2);
  55. }
  56. {
  57. typedef std::tuple<long, char> T0;
  58. typedef std::tuple<long long, int> T1;
  59. T0 t0(2, 'a');
  60. T1 t1 = std::move(t0);
  61. assert(std::get<0>(t1) == 2);
  62. assert(std::get<1>(t1) == int('a'));
  63. }
  64. {
  65. typedef std::tuple<long, char, D> T0;
  66. typedef std::tuple<long long, int, B> T1;
  67. T0 t0(2, 'a', D(3));
  68. T1 t1 = std::move(t0);
  69. assert(std::get<0>(t1) == 2);
  70. assert(std::get<1>(t1) == int('a'));
  71. assert(std::get<2>(t1).id_ == 3);
  72. }
  73. {
  74. D d(3);
  75. typedef std::tuple<long, char, D&> T0;
  76. typedef std::tuple<long long, int, B&> T1;
  77. T0 t0(2, 'a', d);
  78. T1 t1 = std::move(t0);
  79. d.id_ = 2;
  80. assert(std::get<0>(t1) == 2);
  81. assert(std::get<1>(t1) == int('a'));
  82. assert(std::get<2>(t1).id_ == 2);
  83. }
  84. {
  85. typedef std::tuple<long, char, std::unique_ptr<D>> T0;
  86. typedef std::tuple<long long, int, std::unique_ptr<B>> T1;
  87. T0 t0(2, 'a', std::unique_ptr<D>(new D(3)));
  88. T1 t1 = std::move(t0);
  89. assert(std::get<0>(t1) == 2);
  90. assert(std::get<1>(t1) == int('a'));
  91. assert(std::get<2>(t1)->id_ == 3);
  92. }
  93. {
  94. std::tuple<int> t1(42);
  95. std::tuple<Explicit> t2(std::move(t1));
  96. assert(std::get<0>(t2).value == 42);
  97. }
  98. {
  99. std::tuple<int> t1(42);
  100. std::tuple<Implicit> t2 = std::move(t1);
  101. assert(std::get<0>(t2).value == 42);
  102. }
  103. return 0;
  104. }