1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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
19 #include "test_macros.h"
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"
26 constexpr bool check_front( double val
)
28 std::array
<double, 3> arr
= {1, 2, 3.5};
29 return arr
.front() == val
;
32 constexpr bool check_back( double val
)
34 std::array
<double, 3> arr
= {1, 2, 3.5};
35 return arr
.back() == val
;
43 typedef std::array
<T
, 3> C
;
46 C::reference r1
= c
.front();
51 C::reference r2
= c
.back();
58 typedef std::array
<T
, 3> C
;
59 const C c
= {1, 2, 3.5};
60 C::const_reference r1
= c
.front();
63 C::const_reference r2
= c
.back();
68 typedef std::array
<T
, 0> 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();
88 typedef std::array
<const T
, 0> 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());
100 TEST_IGNORE_NODISCARD c
.front();
101 TEST_IGNORE_NODISCARD c
.back();
102 TEST_IGNORE_NODISCARD cc
.front();
103 TEST_IGNORE_NODISCARD cc
.back();
106 #if TEST_STD_VER > 11
109 typedef std::array
<T
, 3> C
;
110 constexpr C c
= {1, 2, 3.5};
112 constexpr T t1
= c
.front();
113 static_assert (t1
== 1, "");
115 constexpr T t2
= c
.back();
116 static_assert (t2
== 3.5, "");
120 #if TEST_STD_VER > 14
122 static_assert (check_front(1), "");
123 static_assert (check_back (3.5), "");