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 int main()
27 {
28     {
29         typedef double T;
30         typedef std::array<T, 3> C;
31         C c = {1, 2, 3.5};
32         C::reference r1 = c.at(0);
33         assert(r1 == 1);
34         r1 = 5.5;
35         assert(c.front() == 5.5);
36 
37         C::reference r2 = c.at(2);
38         assert(r2 == 3.5);
39         r2 = 7.5;
40         assert(c.back() == 7.5);
41 
42         try { (void) c.at(3); }
43         catch (const std::out_of_range &) {}
44     }
45     {
46         typedef double T;
47         typedef std::array<T, 3> C;
48         const C c = {1, 2, 3.5};
49         C::const_reference r1 = c.at(0);
50         assert(r1 == 1);
51 
52         C::const_reference r2 = c.at(2);
53         assert(r2 == 3.5);
54 
55         try { (void) c.at(3); }
56         catch (const std::out_of_range &) {}
57     }
58 
59 #if TEST_STD_VER > 11
60     {
61         typedef double T;
62         typedef std::array<T, 3> C;
63         constexpr C c = {1, 2, 3.5};
64 
65         constexpr T t1 = c.at(0);
66         static_assert (t1 == 1, "");
67 
68         constexpr T t2 = c.at(2);
69         static_assert (t2 == 3.5, "");
70     }
71 #endif
72 
73 }
74