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 front();       // constexpr in C++17
12 // reference back();        // constexpr in C++17
13 // const_reference front(); // constexpr in C++14
14 // const_reference back();  // 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_front( double val )
27 {
28     std::array<double, 3> arr = {1, 2, 3.5};
29     return arr.front() == val;
30 }
31 
32 constexpr bool check_back( double val )
33 {
34     std::array<double, 3> arr = {1, 2, 3.5};
35     return arr.back() == val;
36 }
37 #endif
38 
39 int main(int, char**)
40 {
41     {
42         typedef double T;
43         typedef std::array<T, 3> C;
44         C c = {1, 2, 3.5};
45 
46         C::reference r1 = c.front();
47         assert(r1 == 1);
48         r1 = 5.5;
49         assert(c[0] == 5.5);
50 
51         C::reference r2 = c.back();
52         assert(r2 == 3.5);
53         r2 = 7.5;
54         assert(c[2] == 7.5);
55     }
56     {
57         typedef double T;
58         typedef std::array<T, 3> C;
59         const C c = {1, 2, 3.5};
60         C::const_reference r1 = c.front();
61         assert(r1 == 1);
62 
63         C::const_reference r2 = c.back();
64         assert(r2 == 3.5);
65     }
66     {
67       typedef double T;
68       typedef std::array<T, 0> C;
69       C c = {};
70       C const& cc = c;
71       ASSERT_SAME_TYPE(decltype( c.back()), typename C::reference);
72       ASSERT_SAME_TYPE(decltype(cc.back()), typename C::const_reference);
73       LIBCPP_ASSERT_NOEXCEPT(    c.back());
74       LIBCPP_ASSERT_NOEXCEPT(   cc.back());
75       ASSERT_SAME_TYPE(decltype( c.front()), typename C::reference);
76       ASSERT_SAME_TYPE(decltype(cc.front()), typename C::const_reference);
77       LIBCPP_ASSERT_NOEXCEPT(    c.front());
78       LIBCPP_ASSERT_NOEXCEPT(   cc.front());
79       if (c.size() > (0)) { // always false
80         TEST_IGNORE_NODISCARD c.front();
81         TEST_IGNORE_NODISCARD c.back();
82         TEST_IGNORE_NODISCARD cc.front();
83         TEST_IGNORE_NODISCARD cc.back();
84       }
85     }
86     {
87       typedef double T;
88       typedef std::array<const T, 0> C;
89       C c = {{}};
90       C const& cc = c;
91       ASSERT_SAME_TYPE(decltype( c.back()), typename C::reference);
92       ASSERT_SAME_TYPE(decltype(cc.back()), typename C::const_reference);
93       LIBCPP_ASSERT_NOEXCEPT(    c.back());
94       LIBCPP_ASSERT_NOEXCEPT(   cc.back());
95       ASSERT_SAME_TYPE(decltype( c.front()), typename C::reference);
96       ASSERT_SAME_TYPE(decltype(cc.front()), typename C::const_reference);
97       LIBCPP_ASSERT_NOEXCEPT(    c.front());
98       LIBCPP_ASSERT_NOEXCEPT(   cc.front());
99       if (c.size() > (0)) {
100         TEST_IGNORE_NODISCARD c.front();
101         TEST_IGNORE_NODISCARD c.back();
102         TEST_IGNORE_NODISCARD cc.front();
103         TEST_IGNORE_NODISCARD cc.back();
104       }
105     }
106 #if TEST_STD_VER > 11
107     {
108         typedef double T;
109         typedef std::array<T, 3> C;
110         constexpr C c = {1, 2, 3.5};
111 
112         constexpr T t1 = c.front();
113         static_assert (t1 == 1, "");
114 
115         constexpr T t2 = c.back();
116         static_assert (t2 == 3.5, "");
117     }
118 #endif
119 
120 #if TEST_STD_VER > 14
121     {
122         static_assert (check_front(1),   "");
123         static_assert (check_back (3.5), "");
124     }
125 #endif
126 
127   return 0;
128 }
129