indexing.pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // <array>
  10. // reference operator[] (size_type)
  11. // const_reference operator[] (size_type); // constexpr in C++14
  12. // reference at (size_type)
  13. // const_reference at (size_type); // constexpr in C++14
  14. #include <array>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. // std::array is explicitly allowed to be initialized with A a = { init-list };.
  18. // Disable the missing braces warning for this reason.
  19. #include "disable_missing_braces_warning.h"
  20. #if TEST_STD_VER > 14
  21. constexpr bool check_idx( size_t idx, double val )
  22. {
  23. std::array<double, 3> arr = {1, 2, 3.5};
  24. return arr[idx] == val;
  25. }
  26. #endif
  27. int main()
  28. {
  29. {
  30. typedef double T;
  31. typedef std::array<T, 3> C;
  32. C c = {1, 2, 3.5};
  33. C::reference r1 = c[0];
  34. assert(r1 == 1);
  35. r1 = 5.5;
  36. assert(c.front() == 5.5);
  37. C::reference r2 = c[2];
  38. assert(r2 == 3.5);
  39. r2 = 7.5;
  40. assert(c.back() == 7.5);
  41. }
  42. {
  43. typedef double T;
  44. typedef std::array<T, 3> C;
  45. const C c = {1, 2, 3.5};
  46. C::const_reference r1 = c[0];
  47. assert(r1 == 1);
  48. C::const_reference r2 = c[2];
  49. assert(r2 == 3.5);
  50. }
  51. #if TEST_STD_VER > 11
  52. {
  53. typedef double T;
  54. typedef std::array<T, 3> C;
  55. constexpr C c = {1, 2, 3.5};
  56. constexpr T t1 = c[0];
  57. static_assert (t1 == 1, "");
  58. constexpr T t2 = c[2];
  59. static_assert (t2 == 3.5, "");
  60. }
  61. #endif
  62. #if TEST_STD_VER > 14
  63. {
  64. static_assert (check_idx(0, 1), "");
  65. static_assert (check_idx(1, 2), "");
  66. static_assert (check_idx(2, 3.5), "");
  67. }
  68. #endif
  69. }