front_back.pass.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // reference front(); // constexpr in C++17
  10. // reference back(); // constexpr in C++17
  11. // const_reference front(); // constexpr in C++14
  12. // const_reference back(); // constexpr in C++14
  13. #include <array>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. // std::array is explicitly allowed to be initialized with A a = { init-list };.
  17. // Disable the missing braces warning for this reason.
  18. #include "disable_missing_braces_warning.h"
  19. #if TEST_STD_VER > 14
  20. constexpr bool check_front( double val )
  21. {
  22. std::array<double, 3> arr = {1, 2, 3.5};
  23. return arr.front() == val;
  24. }
  25. constexpr bool check_back( double val )
  26. {
  27. std::array<double, 3> arr = {1, 2, 3.5};
  28. return arr.back() == val;
  29. }
  30. #endif
  31. int main(int, char**)
  32. {
  33. {
  34. typedef double T;
  35. typedef std::array<T, 3> C;
  36. C c = {1, 2, 3.5};
  37. C::reference r1 = c.front();
  38. assert(r1 == 1);
  39. r1 = 5.5;
  40. assert(c[0] == 5.5);
  41. C::reference r2 = c.back();
  42. assert(r2 == 3.5);
  43. r2 = 7.5;
  44. assert(c[2] == 7.5);
  45. }
  46. {
  47. typedef double T;
  48. typedef std::array<T, 3> C;
  49. const C c = {1, 2, 3.5};
  50. C::const_reference r1 = c.front();
  51. assert(r1 == 1);
  52. C::const_reference r2 = c.back();
  53. assert(r2 == 3.5);
  54. }
  55. {
  56. typedef double T;
  57. typedef std::array<T, 0> C;
  58. C c = {};
  59. C const& cc = c;
  60. ASSERT_SAME_TYPE(decltype( c.back()), typename C::reference);
  61. ASSERT_SAME_TYPE(decltype(cc.back()), typename C::const_reference);
  62. LIBCPP_ASSERT_NOEXCEPT( c.back());
  63. LIBCPP_ASSERT_NOEXCEPT( cc.back());
  64. ASSERT_SAME_TYPE(decltype( c.front()), typename C::reference);
  65. ASSERT_SAME_TYPE(decltype(cc.front()), typename C::const_reference);
  66. LIBCPP_ASSERT_NOEXCEPT( c.front());
  67. LIBCPP_ASSERT_NOEXCEPT( cc.front());
  68. if (c.size() > (0)) { // always false
  69. TEST_IGNORE_NODISCARD c.front();
  70. TEST_IGNORE_NODISCARD c.back();
  71. TEST_IGNORE_NODISCARD cc.front();
  72. TEST_IGNORE_NODISCARD cc.back();
  73. }
  74. }
  75. {
  76. typedef double T;
  77. typedef std::array<const T, 0> C;
  78. C c = {{}};
  79. C const& cc = c;
  80. ASSERT_SAME_TYPE(decltype( c.back()), typename C::reference);
  81. ASSERT_SAME_TYPE(decltype(cc.back()), typename C::const_reference);
  82. LIBCPP_ASSERT_NOEXCEPT( c.back());
  83. LIBCPP_ASSERT_NOEXCEPT( cc.back());
  84. ASSERT_SAME_TYPE(decltype( c.front()), typename C::reference);
  85. ASSERT_SAME_TYPE(decltype(cc.front()), typename C::const_reference);
  86. LIBCPP_ASSERT_NOEXCEPT( c.front());
  87. LIBCPP_ASSERT_NOEXCEPT( cc.front());
  88. if (c.size() > (0)) {
  89. TEST_IGNORE_NODISCARD c.front();
  90. TEST_IGNORE_NODISCARD c.back();
  91. TEST_IGNORE_NODISCARD cc.front();
  92. TEST_IGNORE_NODISCARD cc.back();
  93. }
  94. }
  95. #if TEST_STD_VER > 11
  96. {
  97. typedef double T;
  98. typedef std::array<T, 3> C;
  99. constexpr C c = {1, 2, 3.5};
  100. constexpr T t1 = c.front();
  101. static_assert (t1 == 1, "");
  102. constexpr T t2 = c.back();
  103. static_assert (t2 == 3.5, "");
  104. }
  105. #endif
  106. #if TEST_STD_VER > 14
  107. {
  108. static_assert (check_front(1), "");
  109. static_assert (check_back (3.5), "");
  110. }
  111. #endif
  112. return 0;
  113. }