1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // reference operator[] (size_type)
12 // const_reference operator[] (size_type); // constexpr in C++14
13 // reference at (size_type)
14 // const_reference at (size_type); // constexpr in C++14
15 // Libc++ marks these as noexcept
20 #include "test_macros.h"
22 // std::array is explicitly allowed to be initialized with A a = { init-list };.
23 // Disable the missing braces warning for this reason.
24 #include "disable_missing_braces_warning.h"
27 constexpr bool check_idx( size_t idx
, double val
)
29 std::array
<double, 3> arr
= {1, 2, 3.5};
30 return arr
[idx
] == val
;
38 typedef std::array
<T
, 3> C
;
40 LIBCPP_ASSERT_NOEXCEPT(c
[0]);
41 ASSERT_SAME_TYPE(C::reference
, decltype(c
[0]));
42 C::reference r1
= c
[0];
45 assert(c
.front() == 5.5);
47 C::reference r2
= c
[2];
50 assert(c
.back() == 7.5);
54 typedef std::array
<T
, 3> C
;
55 const C c
= {1, 2, 3.5};
56 LIBCPP_ASSERT_NOEXCEPT(c
[0]);
57 ASSERT_SAME_TYPE(C::const_reference
, decltype(c
[0]));
58 C::const_reference r1
= c
[0];
60 C::const_reference r2
= c
[2];
63 { // Test operator[] "works" on zero sized arrays
65 typedef std::array
<T
, 0> C
;
68 LIBCPP_ASSERT_NOEXCEPT(c
[0]);
69 LIBCPP_ASSERT_NOEXCEPT(cc
[0]);
70 ASSERT_SAME_TYPE(C::reference
, decltype(c
[0]));
71 ASSERT_SAME_TYPE(C::const_reference
, decltype(cc
[0]));
72 if (c
.size() > (0)) { // always false
73 C::reference r1
= c
[0];
74 C::const_reference r2
= cc
[0];
79 { // Test operator[] "works" on zero sized arrays
81 typedef std::array
<const T
, 0> C
;
84 LIBCPP_ASSERT_NOEXCEPT(c
[0]);
85 LIBCPP_ASSERT_NOEXCEPT(cc
[0]);
86 ASSERT_SAME_TYPE(C::reference
, decltype(c
[0]));
87 ASSERT_SAME_TYPE(C::const_reference
, decltype(cc
[0]));
88 if (c
.size() > (0)) { // always false
89 C::reference r1
= c
[0];
90 C::const_reference r2
= cc
[0];
98 typedef std::array
<T
, 3> C
;
99 constexpr C c
= {1, 2, 3.5};
100 LIBCPP_ASSERT_NOEXCEPT(c
[0]);
101 ASSERT_SAME_TYPE(C::const_reference
, decltype(c
[0]));
103 constexpr T t1
= c
[0];
104 static_assert (t1
== 1, "");
106 constexpr T t2
= c
[2];
107 static_assert (t2
== 3.5, "");
111 #if TEST_STD_VER > 14
113 static_assert (check_idx(0, 1), "");
114 static_assert (check_idx(1, 2), "");
115 static_assert (check_idx(2, 3.5), "");