1 // <range_access.h> -*- C++ -*-
3 // Copyright (C) 2010-2015 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/range_access.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{iterator}
30 #ifndef _GLIBCXX_RANGE_ACCESS_H
31 #define _GLIBCXX_RANGE_ACCESS_H 1
33 #pragma GCC system_header
35 #if __cplusplus >= 201103L
36 #include <initializer_list>
37 namespace std
_GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 * @brief Return an iterator pointing to the first element of
44 * @param __cont Container.
46 template<typename _Container
>
48 begin(_Container
& __cont
) -> decltype(__cont
.begin())
49 { return __cont
.begin(); }
52 * @brief Return an iterator pointing to the first element of
53 * the const container.
54 * @param __cont Container.
56 template<typename _Container
>
58 begin(const _Container
& __cont
) -> decltype(__cont
.begin())
59 { return __cont
.begin(); }
62 * @brief Return an iterator pointing to one past the last element of
64 * @param __cont Container.
66 template<typename _Container
>
68 end(_Container
& __cont
) -> decltype(__cont
.end())
69 { return __cont
.end(); }
72 * @brief Return an iterator pointing to one past the last element of
73 * the const container.
74 * @param __cont Container.
76 template<typename _Container
>
78 end(const _Container
& __cont
) -> decltype(__cont
.end())
79 { return __cont
.end(); }
82 * @brief Return an iterator pointing to the first element of the array.
85 template<typename _Tp
, size_t _Nm
>
86 inline _GLIBCXX14_CONSTEXPR _Tp
*
87 begin(_Tp (&__arr
)[_Nm
])
91 * @brief Return an iterator pointing to one past the last element
95 template<typename _Tp
, size_t _Nm
>
96 inline _GLIBCXX14_CONSTEXPR _Tp
*
97 end(_Tp (&__arr
)[_Nm
])
98 { return __arr
+ _Nm
; }
100 #if __cplusplus >= 201402L
102 * @brief Return an iterator pointing to the first element of
103 * the const container.
104 * @param __cont Container.
106 template<typename _Container
>
107 inline constexpr auto
108 cbegin(const _Container
& __cont
) noexcept(noexcept(std::begin(__cont
)))
109 -> decltype(std::begin(__cont
))
110 { return std::begin(__cont
); }
113 * @brief Return an iterator pointing to one past the last element of
114 * the const container.
115 * @param __cont Container.
117 template<typename _Container
>
118 inline constexpr auto
119 cend(const _Container
& __cont
) noexcept(noexcept(std::end(__cont
)))
120 -> decltype(std::end(__cont
))
121 { return std::end(__cont
); }
124 * @brief Return a reverse iterator pointing to the last element of
126 * @param __cont Container.
128 template<typename _Container
>
130 rbegin(_Container
& __cont
) -> decltype(__cont
.rbegin())
131 { return __cont
.rbegin(); }
134 * @brief Return a reverse iterator pointing to the last element of
135 * the const container.
136 * @param __cont Container.
138 template<typename _Container
>
140 rbegin(const _Container
& __cont
) -> decltype(__cont
.rbegin())
141 { return __cont
.rbegin(); }
144 * @brief Return a reverse iterator pointing one past the first element of
146 * @param __cont Container.
148 template<typename _Container
>
150 rend(_Container
& __cont
) -> decltype(__cont
.rend())
151 { return __cont
.rend(); }
154 * @brief Return a reverse iterator pointing one past the first element of
155 * the const container.
156 * @param __cont Container.
158 template<typename _Container
>
160 rend(const _Container
& __cont
) -> decltype(__cont
.rend())
161 { return __cont
.rend(); }
164 * @brief Return a reverse iterator pointing to the last element of
166 * @param __arr Array.
168 template<typename _Tp
, size_t _Nm
>
169 inline reverse_iterator
<_Tp
*>
170 rbegin(_Tp (&__arr
)[_Nm
])
171 { return reverse_iterator
<_Tp
*>(__arr
+ _Nm
); }
174 * @brief Return a reverse iterator pointing one past the first element of
176 * @param __arr Array.
178 template<typename _Tp
, size_t _Nm
>
179 inline reverse_iterator
<_Tp
*>
180 rend(_Tp (&__arr
)[_Nm
])
181 { return reverse_iterator
<_Tp
*>(__arr
); }
184 * @brief Return a reverse iterator pointing to the last element of
185 * the initializer_list.
186 * @param __il initializer_list.
188 template<typename _Tp
>
189 inline reverse_iterator
<const _Tp
*>
190 rbegin(initializer_list
<_Tp
> __il
)
191 { return reverse_iterator
<const _Tp
*>(__il
.end()); }
194 * @brief Return a reverse iterator pointing one past the first element of
195 * the initializer_list.
196 * @param __il initializer_list.
198 template<typename _Tp
>
199 inline reverse_iterator
<const _Tp
*>
200 rend(initializer_list
<_Tp
> __il
)
201 { return reverse_iterator
<const _Tp
*>(__il
.begin()); }
204 * @brief Return a reverse iterator pointing to the last element of
205 * the const container.
206 * @param __cont Container.
208 template<typename _Container
>
210 crbegin(const _Container
& __cont
) -> decltype(std::rbegin(__cont
))
211 { return std::rbegin(__cont
); }
214 * @brief Return a reverse iterator pointing one past the first element of
215 * the const container.
216 * @param __cont Container.
218 template<typename _Container
>
220 crend(const _Container
& __cont
) -> decltype(std::rend(__cont
))
221 { return std::rend(__cont
); }
225 #if __cplusplus > 201402L
226 #define __cpp_lib_nonmember_container_access 201411
229 * @brief Return the size of a container.
230 * @param __cont Container.
232 template <typename _Container
>
234 size(const _Container
& __cont
) -> decltype(__cont
.size())
235 { return __cont
.size(); }
238 * @brief Return the size of an array.
239 * @param __array Array.
241 template <typename _Tp
, size_t _Nm
>
243 size(const _Tp (&/*__array*/)[_Nm
]) noexcept
247 * @brief Return whether a container is empty.
248 * @param __cont Container.
250 template <typename _Container
>
252 empty(const _Container
& __cont
) -> decltype(__cont
.empty())
253 { return __cont
.empty(); }
256 * @brief Return whether an array is empty (always false).
257 * @param __array Container.
259 template <typename _Tp
, size_t _Nm
>
261 empty(const _Tp (&/*__array*/)[_Nm
]) noexcept
265 * @brief Return whether an initializer_list is empty.
266 * @param __il Initializer list.
268 template <typename _Tp
>
270 empty(initializer_list
<_Tp
> __il
) noexcept
271 { return __il
.size() == 0;}
274 * @brief Return the data pointer of a container.
275 * @param __cont Container.
277 template <typename _Container
>
279 data(_Container
& __cont
) -> decltype(__cont
.data())
280 { return __cont
.data(); }
283 * @brief Return the data pointer of a const container.
284 * @param __cont Container.
286 template <typename _Container
>
288 data(const _Container
& __cont
) -> decltype(__cont
.data())
289 { return __cont
.data(); }
292 * @brief Return the data pointer of an array.
293 * @param __array Array.
295 template <typename _Tp
, size_t _Nm
>
297 data(_Tp (&__array
)[_Nm
]) noexcept
301 * @brief Return the data pointer of an initializer list.
302 * @param __il Initializer list.
304 template <typename _Tp
>
306 data(initializer_list
<_Tp
> __il
) noexcept
307 { return __il
.begin(); }
311 _GLIBCXX_END_NAMESPACE_VERSION
316 #endif // _GLIBCXX_RANGE_ACCESS_H