1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <array> 11 12 // reference operator[] (size_type) 13 // const_reference operator[] (size_type); // constexpr in C++14 14 // reference at (size_type) 15 // const_reference at (size_type); // constexpr in C++14 16 17 #include <array> 18 #include <cassert> 19 20 #include "test_macros.h" 21 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" 25 26 #if TEST_STD_VER > 14 27 constexpr bool check_idx( size_t idx, double val ) 28 { 29 std::array<double, 3> arr = {1, 2, 3.5}; 30 return arr.at(idx) == val; 31 } 32 #endif 33 34 int main() 35 { 36 { 37 typedef double T; 38 typedef std::array<T, 3> C; 39 C c = {1, 2, 3.5}; 40 C::reference r1 = c.at(0); 41 assert(r1 == 1); 42 r1 = 5.5; 43 assert(c.front() == 5.5); 44 45 C::reference r2 = c.at(2); 46 assert(r2 == 3.5); 47 r2 = 7.5; 48 assert(c.back() == 7.5); 49 50 #ifndef TEST_HAS_NO_EXCEPTIONS 51 try 52 { 53 TEST_IGNORE_NODISCARD c.at(3); 54 assert(false); 55 } 56 catch (const std::out_of_range &) {} 57 #endif 58 } 59 #ifndef TEST_HAS_NO_EXCEPTIONS 60 { 61 typedef double T; 62 typedef std::array<T, 0> C; 63 C c = {}; 64 C const& cc = c; 65 try 66 { 67 TEST_IGNORE_NODISCARD c.at(0); 68 assert(false); 69 } 70 catch (const std::out_of_range &) {} 71 try 72 { 73 TEST_IGNORE_NODISCARD cc.at(0); 74 assert(false); 75 } 76 catch (const std::out_of_range &) {} 77 } 78 #endif 79 { 80 typedef double T; 81 typedef std::array<T, 3> C; 82 const C c = {1, 2, 3.5}; 83 C::const_reference r1 = c.at(0); 84 assert(r1 == 1); 85 86 C::const_reference r2 = c.at(2); 87 assert(r2 == 3.5); 88 89 #ifndef TEST_HAS_NO_EXCEPTIONS 90 try 91 { 92 TEST_IGNORE_NODISCARD c.at(3); 93 assert(false); 94 } 95 catch (const std::out_of_range &) {} 96 #endif 97 } 98 99 #if TEST_STD_VER > 11 100 { 101 typedef double T; 102 typedef std::array<T, 3> C; 103 constexpr C c = {1, 2, 3.5}; 104 105 constexpr T t1 = c.at(0); 106 static_assert (t1 == 1, ""); 107 108 constexpr T t2 = c.at(2); 109 static_assert (t2 == 3.5, ""); 110 } 111 #endif 112 113 #if TEST_STD_VER > 14 114 { 115 static_assert (check_idx(0, 1), ""); 116 static_assert (check_idx(1, 2), ""); 117 static_assert (check_idx(2, 3.5), ""); 118 } 119 #endif 120 } 121