| 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 | |
| 9 | // <array> |
| 10 | |
| 11 | // const_reference operator[](size_type) const; // constexpr in C++14 |
| 12 | // Libc++ marks it as noexcept |
| 13 | |
| 14 | #include <array> |
| 15 | #include <cassert> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | |
| 19 | TEST_CONSTEXPR_CXX14 bool tests() { |
| 20 | { |
| 21 | typedef double T; |
| 22 | typedef std::array<T, 3> C; |
| 23 | C const c = {1, 2, 3.5}; |
| 24 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 25 | ASSERT_SAME_TYPE(C::const_reference, decltype(c[0])); |
| 26 | C::const_reference r1 = c[0]; |
| 27 | assert(r1 == 1); |
| 28 | C::const_reference r2 = c[2]; |
| 29 | assert(r2 == 3.5); |
| 30 | } |
| 31 | // Test operator[] "works" on zero sized arrays |
| 32 | { |
| 33 | { |
| 34 | typedef double T; |
| 35 | typedef std::array<T, 0> C; |
| 36 | C const c = {}; |
| 37 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 38 | ASSERT_SAME_TYPE(C::const_reference, decltype(c[0])); |
| 39 | if (c.size() > (0)) { // always false |
| 40 | C::const_reference r = c[0]; |
| 41 | (void)r; |
| 42 | } |
| 43 | } |
| 44 | { |
| 45 | typedef double T; |
| 46 | typedef std::array<T const, 0> C; |
| 47 | C const c = {}; |
| 48 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 49 | ASSERT_SAME_TYPE(C::const_reference, decltype(c[0])); |
| 50 | if (c.size() > (0)) { // always false |
| 51 | C::const_reference r = c[0]; |
| 52 | (void)r; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | int main(int, char**) { |
| 61 | tests(); |
| 62 | #if TEST_STD_VER >= 14 |
| 63 | static_assert(tests(), "" ); |
| 64 | #endif |
| 65 | return 0; |
| 66 | } |
| 67 | |