convert_copy.pass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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>
  11. // tuple& operator=(const tuple<UTypes...>& u);
  12. // UNSUPPORTED: c++98, c++03
  13. #include <tuple>
  14. #include <string>
  15. #include <cassert>
  16. struct B
  17. {
  18. int id_;
  19. explicit B(int i = 0) : id_(i) {}
  20. };
  21. struct D
  22. : B
  23. {
  24. explicit D(int i = 0) : B(i) {}
  25. };
  26. int main(int, char**)
  27. {
  28. {
  29. typedef std::tuple<long> T0;
  30. typedef std::tuple<long long> T1;
  31. T0 t0(2);
  32. T1 t1;
  33. t1 = t0;
  34. assert(std::get<0>(t1) == 2);
  35. }
  36. {
  37. typedef std::tuple<long, char> T0;
  38. typedef std::tuple<long long, int> T1;
  39. T0 t0(2, 'a');
  40. T1 t1;
  41. t1 = t0;
  42. assert(std::get<0>(t1) == 2);
  43. assert(std::get<1>(t1) == int('a'));
  44. }
  45. {
  46. typedef std::tuple<long, char, D> T0;
  47. typedef std::tuple<long long, int, B> T1;
  48. T0 t0(2, 'a', D(3));
  49. T1 t1;
  50. t1 = t0;
  51. assert(std::get<0>(t1) == 2);
  52. assert(std::get<1>(t1) == int('a'));
  53. assert(std::get<2>(t1).id_ == 3);
  54. }
  55. {
  56. D d(3);
  57. D d2(2);
  58. typedef std::tuple<long, char, D&> T0;
  59. typedef std::tuple<long long, int, B&> T1;
  60. T0 t0(2, 'a', d2);
  61. T1 t1(1, 'b', d);
  62. t1 = t0;
  63. assert(std::get<0>(t1) == 2);
  64. assert(std::get<1>(t1) == int('a'));
  65. assert(std::get<2>(t1).id_ == 2);
  66. }
  67. {
  68. // Test that tuple evaluates correctly applies an lvalue reference
  69. // before evaluating is_assignable (ie 'is_assignable<int&, int&>')
  70. // instead of evaluating 'is_assignable<int&&, int&>' which is false.
  71. int x = 42;
  72. int y = 43;
  73. std::tuple<int&&> t(std::move(x));
  74. std::tuple<int&> t2(y);
  75. t = t2;
  76. assert(std::get<0>(t) == 43);
  77. assert(&std::get<0>(t) == &x);
  78. }
  79. return 0;
  80. }