xref: /llvm-project/libcxx/test/std/containers/sequences/array/indexing.pass.cpp (revision b4e2e7a2922229393d10a6de131f7f60e4ea0057)
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[0];
33         assert(r1 == 1);
34         r1 = 5.5;
35         assert(c.front() == 5.5);
36 
37         C::reference r2 = c[2];
38         assert(r2 == 3.5);
39         r2 = 7.5;
40         assert(c.back() == 7.5);
41     }
42     {
43         typedef double T;
44         typedef std::array<T, 3> C;
45         const C c = {1, 2, 3.5};
46         C::const_reference r1 = c[0];
47         assert(r1 == 1);
48         C::const_reference r2 = c[2];
49         assert(r2 == 3.5);
50     }
51 
52 #if TEST_STD_VER > 11
53     {
54         typedef double T;
55         typedef std::array<T, 3> C;
56         constexpr C c = {1, 2, 3.5};
57 
58         constexpr T t1 = c[0];
59         static_assert (t1 == 1, "");
60 
61         constexpr T t2 = c[2];
62         static_assert (t2 == 3.5, "");
63     }
64 #endif
65 
66 }
67