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 
14 #include <array>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 
19 // std::array is explicitly allowed to be initialized with A a = { init-list };.
20 // Disable the missing braces warning for this reason.
21 #include "disable_missing_braces_warning.h"
22 
23 
tests()24 TEST_CONSTEXPR_CXX17 bool tests()
25 {
26     {
27         typedef double T;
28         typedef std::array<T, 3> C;
29         C c = {1, 2, 3.5};
30 
31         C::reference r1 = c.front();
32         assert(r1 == 1);
33         r1 = 5.5;
34         assert(c[0] == 5.5);
35 
36         C::reference r2 = c.back();
37         assert(r2 == 3.5);
38         r2 = 7.5;
39         assert(c[2] == 7.5);
40     }
41     {
42         typedef double T;
43         typedef std::array<T, 0> C;
44         C c = {};
45         ASSERT_SAME_TYPE(decltype(c.back()), C::reference);
46         LIBCPP_ASSERT_NOEXCEPT(c.back());
47         ASSERT_SAME_TYPE(decltype(c.front()), C::reference);
48         LIBCPP_ASSERT_NOEXCEPT(c.front());
49         if (c.size() > (0)) { // always false
50             TEST_IGNORE_NODISCARD c.front();
51             TEST_IGNORE_NODISCARD c.back();
52         }
53     }
54     {
55         typedef double T;
56         typedef std::array<const T, 0> C;
57         C c = {};
58         ASSERT_SAME_TYPE(decltype( c.back()), C::reference);
59         LIBCPP_ASSERT_NOEXCEPT(    c.back());
60         ASSERT_SAME_TYPE(decltype( c.front()), C::reference);
61         LIBCPP_ASSERT_NOEXCEPT(    c.front());
62         if (c.size() > (0)) {
63             TEST_IGNORE_NODISCARD c.front();
64             TEST_IGNORE_NODISCARD c.back();
65         }
66     }
67 
68     return true;
69 }
70 
main(int,char **)71 int main(int, char**)
72 {
73     tests();
74 #if TEST_STD_VER >= 17
75     static_assert(tests(), "");
76 #endif
77     return 0;
78 }
79