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 // std::array is explicitly allowed to be initialized with A a = { init-list };. 20 // Disable the missing braces warning for this reason. 21 #include "disable_missing_braces_warning.h" 22 23 24 TEST_CONSTEXPR_CXX17 bool tests() 25 { 26 { 27 typedef double T; 28 typedef std::array<T, 3> C; 29 C c = {1, 2, 3.5}; 30 LIBCPP_ASSERT_NOEXCEPT(c[0]); 31 ASSERT_SAME_TYPE(C::reference, decltype(c[0])); 32 C::reference r1 = c[0]; 33 assert(r1 == 1); 34 r1 = 5.5; 35 assert(c.front() == 5.5); 36 37 C::reference r2 = c[2]; 38 assert(r2 == 3.5); 39 r2 = 7.5; 40 assert(c.back() == 7.5); 41 } 42 43 // Test operator[] "works" on zero sized arrays 44 { 45 { 46 typedef double T; 47 typedef std::array<T, 0> C; 48 C c = {}; 49 LIBCPP_ASSERT_NOEXCEPT(c[0]); 50 ASSERT_SAME_TYPE(C::reference, decltype(c[0])); 51 if (c.size() > (0)) { // always false 52 C::reference r = c[0]; 53 (void)r; 54 } 55 } 56 { 57 typedef double T; 58 typedef std::array<const T, 0> C; 59 C c = {}; 60 LIBCPP_ASSERT_NOEXCEPT(c[0]); 61 ASSERT_SAME_TYPE(C::reference, decltype(c[0])); 62 if (c.size() > (0)) { // always false 63 C::reference r = c[0]; 64 (void)r; 65 } 66 } 67 } 68 69 return true; 70 } 71 72 int main(int, char**) 73 { 74 tests(); 75 #if TEST_STD_VER >= 17 76 static_assert(tests(), ""); 77 #endif 78 return 0; 79 } 80