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