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 // const_reference front() const; // constexpr in C++14
12 // const_reference back() const;  // constexpr in C++14
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_CXX14 bool tests()
25 {
26     {
27         typedef double T;
28         typedef std::array<T, 3> C;
29         C const c = {1, 2, 3.5};
30         C::const_reference r1 = c.front();
31         assert(r1 == 1);
32 
33         C::const_reference r2 = c.back();
34         assert(r2 == 3.5);
35     }
36     {
37         typedef double T;
38         typedef std::array<T, 0> C;
39         C const c = {};
40         ASSERT_SAME_TYPE(decltype(c.back()), C::const_reference);
41         LIBCPP_ASSERT_NOEXCEPT(c.back());
42         ASSERT_SAME_TYPE(decltype(c.front()), C::const_reference);
43         LIBCPP_ASSERT_NOEXCEPT(c.front());
44         if (c.size() > (0)) { // always false
45             TEST_IGNORE_NODISCARD c.front();
46             TEST_IGNORE_NODISCARD c.back();
47         }
48     }
49     {
50         typedef double T;
51         typedef std::array<const T, 0> C;
52         C const c = {};
53         ASSERT_SAME_TYPE(decltype(c.back()), C::const_reference);
54         LIBCPP_ASSERT_NOEXCEPT(c.back());
55         ASSERT_SAME_TYPE(decltype(c.front()), C::const_reference);
56         LIBCPP_ASSERT_NOEXCEPT(c.front());
57         if (c.size() > (0)) {
58             TEST_IGNORE_NODISCARD c.front();
59             TEST_IGNORE_NODISCARD c.back();
60         }
61     }
62 
63     return true;
64 }
65 
main(int,char **)66 int main(int, char**)
67 {
68     tests();
69 #if TEST_STD_VER >= 14
70     static_assert(tests(), "");
71 #endif
72     return 0;
73 }
74