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 // UNSUPPORTED: c++03
10
11 // <initializer_list>
12
13 // template<class E> constexpr const E* begin(initializer_list<E> il) noexcept; // constexpr since C++14
14 // template<class E> constexpr const E* end(initializer_list<E> il) noexcept; // constexpr since C++14
15
16 #include <initializer_list>
17 #include <cassert>
18 #include <cstddef>
19
20 #include "test_macros.h"
21
test()22 TEST_CONSTEXPR_CXX14 bool test() {
23 // unqualified begin/end
24 {
25 std::initializer_list<int> il = {3, 2, 1};
26 ASSERT_NOEXCEPT(begin(il));
27 ASSERT_NOEXCEPT(end(il));
28 ASSERT_SAME_TYPE(decltype(begin(il)), const int*);
29 ASSERT_SAME_TYPE(decltype(end(il)), const int*);
30 const int* b = begin(il);
31 const int* e = end(il);
32 assert(il.size() == 3);
33 assert(static_cast<std::size_t>(e - b) == il.size());
34 assert(*b++ == 3);
35 assert(*b++ == 2);
36 assert(*b++ == 1);
37 }
38
39 // qualified begin/end
40 {
41 std::initializer_list<int> il = {1, 2, 3};
42 ASSERT_NOEXCEPT(std::begin(il));
43 ASSERT_NOEXCEPT(std::end(il));
44 ASSERT_SAME_TYPE(decltype(std::begin(il)), const int*);
45 ASSERT_SAME_TYPE(decltype(std::end(il)), const int*);
46 assert(std::begin(il) == il.begin());
47 assert(std::end(il) == il.end());
48
49 const auto& cil = il;
50 ASSERT_NOEXCEPT(std::begin(cil));
51 ASSERT_NOEXCEPT(std::end(cil));
52 ASSERT_SAME_TYPE(decltype(std::begin(cil)), const int*);
53 ASSERT_SAME_TYPE(decltype(std::end(cil)), const int*);
54 assert(std::begin(cil) == il.begin());
55 assert(std::end(cil) == il.end());
56 }
57
58 return true;
59 }
60
main(int,char **)61 int main(int, char**) {
62 test();
63 #if TEST_STD_VER >= 14
64 static_assert(test(), "");
65 #endif
66
67 return 0;
68 }
69