get_const.pass.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 const&
  12. // get(const tuple<Types...>& t);
  13. // UNSUPPORTED: c++98, c++03
  14. #include <tuple>
  15. #include <string>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. struct Empty {};
  19. int main(int, char**)
  20. {
  21. {
  22. typedef std::tuple<int> T;
  23. const T t(3);
  24. assert(std::get<0>(t) == 3);
  25. }
  26. {
  27. typedef std::tuple<std::string, int> T;
  28. const T t("high", 5);
  29. assert(std::get<0>(t) == "high");
  30. assert(std::get<1>(t) == 5);
  31. }
  32. #if TEST_STD_VER > 11
  33. {
  34. typedef std::tuple<double, int> T;
  35. constexpr T t(2.718, 5);
  36. static_assert(std::get<0>(t) == 2.718, "");
  37. static_assert(std::get<1>(t) == 5, "");
  38. }
  39. {
  40. typedef std::tuple<Empty> T;
  41. constexpr T t{Empty()};
  42. constexpr Empty e = std::get<0>(t);
  43. ((void)e); // Prevent unused warning
  44. }
  45. #endif
  46. {
  47. typedef std::tuple<double&, std::string, int> T;
  48. double d = 1.5;
  49. const T t(d, "high", 5);
  50. assert(std::get<0>(t) == 1.5);
  51. assert(std::get<1>(t) == "high");
  52. assert(std::get<2>(t) == 5);
  53. std::get<0>(t) = 2.5;
  54. assert(std::get<0>(t) == 2.5);
  55. assert(std::get<1>(t) == "high");
  56. assert(std::get<2>(t) == 5);
  57. assert(d == 2.5);
  58. }
  59. return 0;
  60. }