at.pass.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.at(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.at(0);
  34. assert(r1 == 1);
  35. r1 = 5.5;
  36. assert(c.front() == 5.5);
  37. C::reference r2 = c.at(2);
  38. assert(r2 == 3.5);
  39. r2 = 7.5;
  40. assert(c.back() == 7.5);
  41. #ifndef TEST_HAS_NO_EXCEPTIONS
  42. try
  43. {
  44. (void) c.at(3);
  45. assert(false);
  46. }
  47. catch (const std::out_of_range &) {}
  48. #endif
  49. }
  50. {
  51. typedef double T;
  52. typedef std::array<T, 3> C;
  53. const C c = {1, 2, 3.5};
  54. C::const_reference r1 = c.at(0);
  55. assert(r1 == 1);
  56. C::const_reference r2 = c.at(2);
  57. assert(r2 == 3.5);
  58. #ifndef TEST_HAS_NO_EXCEPTIONS
  59. try
  60. {
  61. (void) c.at(3);
  62. assert(false);
  63. }
  64. catch (const std::out_of_range &) {}
  65. #endif
  66. }
  67. #if TEST_STD_VER > 11
  68. {
  69. typedef double T;
  70. typedef std::array<T, 3> C;
  71. constexpr C c = {1, 2, 3.5};
  72. constexpr T t1 = c.at(0);
  73. static_assert (t1 == 1, "");
  74. constexpr T t2 = c.at(2);
  75. static_assert (t2 == 3.5, "");
  76. }
  77. #endif
  78. #if TEST_STD_VER > 14
  79. {
  80. static_assert (check_idx(0, 1), "");
  81. static_assert (check_idx(1, 2), "");
  82. static_assert (check_idx(2, 3.5), "");
  83. }
  84. #endif
  85. }