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