get_const.pass.cpp 1.7 KB

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