get_non_const.pass.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <tuple>
  10. // template <class... Types> class tuple;
  11. // template <size_t I, class... Types>
  12. // typename tuple_element<I, tuple<Types...> >::type&
  13. // get(tuple<Types...>& t);
  14. // UNSUPPORTED: c++98, c++03
  15. #include <tuple>
  16. #include <string>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. #if TEST_STD_VER > 11
  20. struct Empty {};
  21. struct S {
  22. std::tuple<int, Empty> a;
  23. int k;
  24. Empty e;
  25. constexpr S() : a{1,Empty{}}, k(std::get<0>(a)), e(std::get<1>(a)) {}
  26. };
  27. constexpr std::tuple<int, int> getP () { return { 3, 4 }; }
  28. #endif
  29. int main()
  30. {
  31. {
  32. typedef std::tuple<int> T;
  33. T t(3);
  34. assert(std::get<0>(t) == 3);
  35. std::get<0>(t) = 2;
  36. assert(std::get<0>(t) == 2);
  37. }
  38. {
  39. typedef std::tuple<std::string, int> T;
  40. T t("high", 5);
  41. assert(std::get<0>(t) == "high");
  42. assert(std::get<1>(t) == 5);
  43. std::get<0>(t) = "four";
  44. std::get<1>(t) = 4;
  45. assert(std::get<0>(t) == "four");
  46. assert(std::get<1>(t) == 4);
  47. }
  48. {
  49. typedef std::tuple<double&, std::string, int> T;
  50. double d = 1.5;
  51. T t(d, "high", 5);
  52. assert(std::get<0>(t) == 1.5);
  53. assert(std::get<1>(t) == "high");
  54. assert(std::get<2>(t) == 5);
  55. std::get<0>(t) = 2.5;
  56. std::get<1>(t) = "four";
  57. std::get<2>(t) = 4;
  58. assert(std::get<0>(t) == 2.5);
  59. assert(std::get<1>(t) == "four");
  60. assert(std::get<2>(t) == 4);
  61. assert(d == 2.5);
  62. }
  63. #if _LIBCPP_STD_VER > 11
  64. { // get on an rvalue tuple
  65. static_assert ( std::get<0> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 0, "" );
  66. static_assert ( std::get<1> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 1, "" );
  67. static_assert ( std::get<2> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 2, "" );
  68. static_assert ( std::get<3> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 3, "" );
  69. static_assert(S().k == 1, "");
  70. static_assert(std::get<1>(getP()) == 4, "");
  71. }
  72. #endif
  73. }