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 // XFAIL: c++03 10 11 // <iterator> 12 // template <class C> constexpr auto begin(C& c) -> decltype(c.begin()); 13 // template <class C> constexpr auto begin(const C& c) -> decltype(c.begin()); 14 // template <class C> constexpr auto cbegin(const C& c) -> decltype(std::begin(c)); // C++14 15 // template <class C> constexpr auto cend(const C& c) -> decltype(std::end(c)); // C++14 16 // template <class C> constexpr auto end (C& c) -> decltype(c.end()); 17 // template <class C> constexpr auto end (const C& c) -> decltype(c.end()); 18 // template <class E> constexpr reverse_iterator<const E*> rbegin(initializer_list<E> il); 19 // template <class E> constexpr reverse_iterator<const E*> rend (initializer_list<E> il); 20 // 21 // template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14 22 // template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14 23 // template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14 24 // template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14 25 // template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14 26 // template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14 27 // template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14 28 // template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14 29 // 30 // All of these are constexpr in C++17 31 32 #include "test_macros.h" 33 34 #include <iterator> 35 #include <cassert> 36 #include <vector> 37 #include <array> 38 #include <list> 39 #include <initializer_list> 40 41 template<typename C> 42 void test_const_container( const C & c, typename C::value_type val ) { 43 assert ( std::begin(c) == c.begin()); 44 assert (*std::begin(c) == val ); 45 assert ( std::begin(c) != c.end()); 46 assert ( std::end(c) == c.end()); 47 #if TEST_STD_VER > 11 48 assert ( std::cbegin(c) == c.cbegin()); 49 assert ( std::cbegin(c) != c.cend()); 50 assert ( std::cend(c) == c.cend()); 51 assert ( std::rbegin(c) == c.rbegin()); 52 assert ( std::rbegin(c) != c.rend()); 53 assert ( std::rend(c) == c.rend()); 54 assert ( std::crbegin(c) == c.crbegin()); 55 assert ( std::crbegin(c) != c.crend()); 56 assert ( std::crend(c) == c.crend()); 57 #endif 58 } 59 60 template<typename T> 61 void test_const_container( const std::initializer_list<T> & c, T val ) { 62 assert ( std::begin(c) == c.begin()); 63 assert (*std::begin(c) == val ); 64 assert ( std::begin(c) != c.end()); 65 assert ( std::end(c) == c.end()); 66 #if TEST_STD_VER > 11 67 // initializer_list doesn't have cbegin/cend/rbegin/rend 68 // but std::cbegin(),etc work (b/c they're general fn templates) 69 // assert ( std::cbegin(c) == c.cbegin()); 70 // assert ( std::cbegin(c) != c.cend()); 71 // assert ( std::cend(c) == c.cend()); 72 // assert ( std::rbegin(c) == c.rbegin()); 73 // assert ( std::rbegin(c) != c.rend()); 74 // assert ( std::rend(c) == c.rend()); 75 // assert ( std::crbegin(c) == c.crbegin()); 76 // assert ( std::crbegin(c) != c.crend()); 77 // assert ( std::crend(c) == c.crend()); 78 #endif 79 } 80 81 template<typename C> 82 void test_container( C & c, typename C::value_type val ) { 83 assert ( std::begin(c) == c.begin()); 84 assert (*std::begin(c) == val ); 85 assert ( std::begin(c) != c.end()); 86 assert ( std::end(c) == c.end()); 87 #if TEST_STD_VER > 11 88 assert ( std::cbegin(c) == c.cbegin()); 89 assert ( std::cbegin(c) != c.cend()); 90 assert ( std::cend(c) == c.cend()); 91 assert ( std::rbegin(c) == c.rbegin()); 92 assert ( std::rbegin(c) != c.rend()); 93 assert ( std::rend(c) == c.rend()); 94 assert ( std::crbegin(c) == c.crbegin()); 95 assert ( std::crbegin(c) != c.crend()); 96 assert ( std::crend(c) == c.crend()); 97 #endif 98 } 99 100 template<typename T> 101 void test_container( std::initializer_list<T> & c, T val ) { 102 assert ( std::begin(c) == c.begin()); 103 assert (*std::begin(c) == val ); 104 assert ( std::begin(c) != c.end()); 105 assert ( std::end(c) == c.end()); 106 #if TEST_STD_VER > 11 107 // initializer_list doesn't have cbegin/cend/rbegin/rend 108 // assert ( std::cbegin(c) == c.cbegin()); 109 // assert ( std::cbegin(c) != c.cend()); 110 // assert ( std::cend(c) == c.cend()); 111 // assert ( std::rbegin(c) == c.rbegin()); 112 // assert ( std::rbegin(c) != c.rend()); 113 // assert ( std::rend(c) == c.rend()); 114 // assert ( std::crbegin(c) == c.crbegin()); 115 // assert ( std::crbegin(c) != c.crend()); 116 // assert ( std::crend(c) == c.crend()); 117 #endif 118 } 119 120 template<typename T, size_t Sz> 121 void test_const_array( const T (&array)[Sz] ) { 122 assert ( std::begin(array) == array ); 123 assert (*std::begin(array) == array[0] ); 124 assert ( std::begin(array) != std::end(array)); 125 assert ( std::end(array) == array + Sz); 126 #if TEST_STD_VER > 11 127 assert ( std::cbegin(array) == array ); 128 assert (*std::cbegin(array) == array[0] ); 129 assert ( std::cbegin(array) != std::cend(array)); 130 assert ( std::cend(array) == array + Sz); 131 #endif 132 } 133 134 int main(int, char**) { 135 std::vector<int> v; v.push_back(1); 136 std::list<int> l; l.push_back(2); 137 std::array<int, 1> a; a[0] = 3; 138 std::initializer_list<int> il = { 4 }; 139 140 test_container ( v, 1 ); 141 test_container ( l, 2 ); 142 test_container ( a, 3 ); 143 test_container ( il, 4 ); 144 145 test_const_container ( v, 1 ); 146 test_const_container ( l, 2 ); 147 test_const_container ( a, 3 ); 148 test_const_container ( il, 4 ); 149 150 static constexpr int arrA [] { 1, 2, 3 }; 151 test_const_array ( arrA ); 152 #if TEST_STD_VER > 11 153 constexpr const int *b = std::cbegin(arrA); 154 constexpr const int *e = std::cend(arrA); 155 static_assert(e - b == 3, ""); 156 #endif 157 158 #if TEST_STD_VER > 14 159 { 160 typedef std::array<int, 5> C; 161 constexpr const C c{0,1,2,3,4}; 162 163 static_assert ( c.begin() == std::begin(c), ""); 164 static_assert ( c.cbegin() == std::cbegin(c), ""); 165 static_assert ( c.end() == std::end(c), ""); 166 static_assert ( c.cend() == std::cend(c), ""); 167 168 static_assert ( c.rbegin() == std::rbegin(c), ""); 169 static_assert ( c.crbegin() == std::crbegin(c), ""); 170 static_assert ( c.rend() == std::rend(c), ""); 171 static_assert ( c.crend() == std::crend(c), ""); 172 173 static_assert ( std::begin(c) != std::end(c), ""); 174 static_assert ( std::rbegin(c) != std::rend(c), ""); 175 static_assert ( std::cbegin(c) != std::cend(c), ""); 176 static_assert ( std::crbegin(c) != std::crend(c), ""); 177 178 static_assert ( *c.begin() == 0, ""); 179 static_assert ( *c.rbegin() == 4, ""); 180 181 static_assert ( *std::begin(c) == 0, "" ); 182 static_assert ( *std::cbegin(c) == 0, "" ); 183 static_assert ( *std::rbegin(c) == 4, "" ); 184 static_assert ( *std::crbegin(c) == 4, "" ); 185 } 186 187 { 188 static constexpr const int c[] = {0,1,2,3,4}; 189 190 static_assert ( *std::begin(c) == 0, "" ); 191 static_assert ( *std::cbegin(c) == 0, "" ); 192 static_assert ( *std::rbegin(c) == 4, "" ); 193 static_assert ( *std::crbegin(c) == 4, "" ); 194 } 195 #endif 196 197 return 0; 198 } 199