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 #ifndef TEST_HAS_NO_EXCEPTIONS
43         try
44         {
45             (void) c.at(3);
46             assert(false);
47         }
48         catch (const std::out_of_range &) {}
49 #endif
50     }
51     {
52         typedef double T;
53         typedef std::array<T, 3> C;
54         const C c = {1, 2, 3.5};
55         C::const_reference r1 = c.at(0);
56         assert(r1 == 1);
57 
58         C::const_reference r2 = c.at(2);
59         assert(r2 == 3.5);
60 
61 #ifndef TEST_HAS_NO_EXCEPTIONS
62         try
63         {
64             (void) c.at(3);
65             assert(false);
66         }
67         catch (const std::out_of_range &) {}
68 #endif
69     }
70 
71 #if TEST_STD_VER > 11
72     {
73         typedef double T;
74         typedef std::array<T, 3> C;
75         constexpr C c = {1, 2, 3.5};
76 
77         constexpr T t1 = c.at(0);
78         static_assert (t1 == 1, "");
79 
80         constexpr T t2 = c.at(2);
81         static_assert (t2 == 3.5, "");
82     }
83 #endif
84 
85 }
86