get_non_const.pass.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <size_t I, class... Types>
  11. // typename tuple_element<I, tuple<Types...> >::type&
  12. // get(tuple<Types...>& t);
  13. // UNSUPPORTED: c++98, c++03
  14. #include <tuple>
  15. #include <string>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #if TEST_STD_VER > 11
  19. struct Empty {};
  20. struct S {
  21. std::tuple<int, Empty> a;
  22. int k;
  23. Empty e;
  24. constexpr S() : a{1,Empty{}}, k(std::get<0>(a)), e(std::get<1>(a)) {}
  25. };
  26. constexpr std::tuple<int, int> getP () { return { 3, 4 }; }
  27. #endif
  28. int main(int, char**)
  29. {
  30. {
  31. typedef std::tuple<int> T;
  32. T t(3);
  33. assert(std::get<0>(t) == 3);
  34. std::get<0>(t) = 2;
  35. assert(std::get<0>(t) == 2);
  36. }
  37. {
  38. typedef std::tuple<std::string, int> T;
  39. T t("high", 5);
  40. assert(std::get<0>(t) == "high");
  41. assert(std::get<1>(t) == 5);
  42. std::get<0>(t) = "four";
  43. std::get<1>(t) = 4;
  44. assert(std::get<0>(t) == "four");
  45. assert(std::get<1>(t) == 4);
  46. }
  47. {
  48. typedef std::tuple<double&, std::string, int> T;
  49. double d = 1.5;
  50. T t(d, "high", 5);
  51. assert(std::get<0>(t) == 1.5);
  52. assert(std::get<1>(t) == "high");
  53. assert(std::get<2>(t) == 5);
  54. std::get<0>(t) = 2.5;
  55. std::get<1>(t) = "four";
  56. std::get<2>(t) = 4;
  57. assert(std::get<0>(t) == 2.5);
  58. assert(std::get<1>(t) == "four");
  59. assert(std::get<2>(t) == 4);
  60. assert(d == 2.5);
  61. }
  62. #if TEST_STD_VER > 11
  63. { // get on an rvalue tuple
  64. static_assert ( std::get<0> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 0, "" );
  65. static_assert ( std::get<1> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 1, "" );
  66. static_assert ( std::get<2> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 2, "" );
  67. static_assert ( std::get<3> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 3, "" );
  68. static_assert(S().k == 1, "");
  69. static_assert(std::get<1>(getP()) == 4, "");
  70. }
  71. #endif
  72. return 0;
  73. }