get.pass.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // <array>
  9. // template <size_t I, class T, size_t N> T& get(array<T, N>& a);
  10. #include <array>
  11. #include <cassert>
  12. #include "test_macros.h"
  13. // std::array is explicitly allowed to be initialized with A a = { init-list };.
  14. // Disable the missing braces warning for this reason.
  15. #include "disable_missing_braces_warning.h"
  16. #if TEST_STD_VER > 11
  17. struct S {
  18. std::array<int, 3> a;
  19. int k;
  20. constexpr S() : a{1,2,3}, k(std::get<2>(a)) {}
  21. };
  22. constexpr std::array<int, 2> getArr () { return { 3, 4 }; }
  23. #endif
  24. int main(int, char**)
  25. {
  26. {
  27. typedef double T;
  28. typedef std::array<T, 3> C;
  29. C c = {1, 2, 3.5};
  30. std::get<1>(c) = 5.5;
  31. assert(c[0] == 1);
  32. assert(c[1] == 5.5);
  33. assert(c[2] == 3.5);
  34. }
  35. #if TEST_STD_VER > 11
  36. {
  37. typedef double T;
  38. typedef std::array<T, 3> C;
  39. constexpr C c = {1, 2, 3.5};
  40. static_assert(std::get<0>(c) == 1, "");
  41. static_assert(std::get<1>(c) == 2, "");
  42. static_assert(std::get<2>(c) == 3.5, "");
  43. }
  44. {
  45. static_assert(S().k == 3, "");
  46. static_assert(std::get<1>(getArr()) == 4, "");
  47. }
  48. #endif
  49. return 0;
  50. }