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