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