convert_copy.pass.cpp 2.2 KB

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