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 //===----------------------------------------------------------------------===//
13 // template <class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
14 // template <class T, size_t N> constexpr T* end(T (&array)[N]) noexcept;
16 // template <class T, size_t N> constexpr reverse_iterator<T*> rbegin(T (&array)[N]); // C++14, constexpr since C++17
17 // template <class T, size_t N> constexpr reverse_iterator<T*> rend(T (&array)[N]); // C++14, constexpr since C++17
22 #include "test_macros.h"
24 TEST_CONSTEXPR_CXX14
bool test() {
28 // std::begin(T (&)[N]) / std::end(T (&)[N])
30 ASSERT_NOEXCEPT(std::begin(a
));
31 ASSERT_SAME_TYPE(decltype(std::begin(a
)), int*);
32 assert(std::begin(a
) == a
);
34 ASSERT_NOEXCEPT(std::end(a
));
35 ASSERT_SAME_TYPE(decltype(std::end(a
)), int*);
36 assert(std::end(a
) == a
+ 3);
38 // kind of overkill since it follows from the definition, but worth testing
39 ASSERT_NOEXCEPT(std::begin(ca
));
40 ASSERT_SAME_TYPE(decltype(std::begin(ca
)), const int*);
41 assert(std::begin(ca
) == ca
);
43 ASSERT_NOEXCEPT(std::end(ca
));
44 ASSERT_SAME_TYPE(decltype(std::end(ca
)), const int*);
45 assert(std::end(ca
) == ca
+ 3);
51 TEST_CONSTEXPR_CXX17
bool test_r() {
52 #if TEST_STD_VER >= 14
56 // std::rbegin(T (&)[N]) / std::rend(T (&)[N])
58 ASSERT_SAME_TYPE(decltype(std::rbegin(a
)), std::reverse_iterator
<int*>);
59 assert(std::rbegin(a
).base() == a
+ 3);
61 ASSERT_SAME_TYPE(decltype(std::rend(a
)), std::reverse_iterator
<int*>);
62 assert(std::rend(a
).base() == a
);
64 // kind of overkill since it follows from the definition, but worth testing
65 ASSERT_SAME_TYPE(decltype(std::rbegin(ca
)), std::reverse_iterator
<const int*>);
66 assert(std::rbegin(ca
).base() == ca
+ 3);
68 ASSERT_SAME_TYPE(decltype(std::rend(ca
)), std::reverse_iterator
<const int*>);
69 assert(std::rend(ca
).base() == ca
);
76 int main(int, char**) {
79 #if TEST_STD_VER >= 14
80 static_assert(test(), "");
82 #if TEST_STD_VER >= 17
83 static_assert(test_r(), "");
86 // Make sure std::begin(T (&)[N]) and std::end(T (&)[N]) are constexpr in C++11 too (see LWG2280).
88 static constexpr int a
[] = {1, 2, 3};
89 constexpr auto b
= std::begin(a
);
91 constexpr auto e
= std::end(a
);