libstdc++
stl_iterator_base_types.h
Go to the documentation of this file.
1// Types used in iterator implementation -*- C++ -*-
2
3// Copyright (C) 2001-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51/** @file bits/stl_iterator_base_types.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{iterator}
54 *
55 * This file contains all of the general iterator-related utility types,
56 * such as iterator_traits and struct iterator.
57 */
58
59#ifndef _STL_ITERATOR_BASE_TYPES_H
60#define _STL_ITERATOR_BASE_TYPES_H 1
61
62#ifdef _GLIBCXX_SYSHDR
63#pragma GCC system_header
64#endif
65
66#include <bits/c++config.h>
67
68#if __cplusplus >= 201103L
69# include <type_traits> // For __void_t, is_convertible, __enable_if_t
70#else
71# include <ext/type_traits.h> // For __gnu_cxx::__enable_if
72#endif
73
74#if __cplusplus > 201703L && __cpp_concepts >= 201907L
76#endif
77
78namespace std _GLIBCXX_VISIBILITY(default)
79{
80_GLIBCXX_BEGIN_NAMESPACE_VERSION
81
82 /**
83 * @defgroup iterators Iterators
84 * Abstractions for uniform iterating through various underlying types.
85 */
86 ///@{
87
88 /**
89 * @defgroup iterator_tags Iterator Tags
90 * These are empty types, used to distinguish different iterators. The
91 * distinction is not made by what they contain, but simply by what they
92 * are. Different underlying algorithms can then be used based on the
93 * different operations supported by different iterator types.
94 */
95 ///@{
96 /// Marking input iterators.
98
99 /// Marking output iterators.
101
102 /// Forward iterators support a superset of input iterator operations.
104
105 /// Bidirectional iterators support a superset of forward iterator
106 /// operations.
108
109 /// Random-access iterators support a superset of bidirectional
110 /// iterator operations.
112
113#if __cplusplus > 201703L
114 /// Contiguous iterators point to objects stored contiguously in memory.
116#endif
117 ///@}
118
119 /**
120 * @brief Common %iterator class.
121 *
122 * This class does nothing but define nested typedefs. %Iterator classes
123 * can inherit from this class to save some work. The typedefs are then
124 * used in specializations and overloading.
125 *
126 * In particular, there are no default implementations of requirements
127 * such as `operator++` and the like. (How could there be?)
128 *
129 * @deprecated Deprecated since C++17. The recommended alternative is to
130 * simply define the typedefs directly in your iterator class.
131 */
132 template<typename _Category, typename _Tp, typename _Distance = ptrdiff_t,
133 typename _Pointer = _Tp*, typename _Reference = _Tp&>
134 struct _GLIBCXX17_DEPRECATED iterator
135 {
136 /// One of the @link iterator_tags tag types@endlink.
137 typedef _Category iterator_category;
138 /// The type "pointed to" by the iterator.
139 typedef _Tp value_type;
140 /// Distance between iterators is represented as this type.
141 typedef _Distance difference_type;
142 /// This type represents a pointer-to-value_type.
143 typedef _Pointer pointer;
144 /// This type represents a reference-to-value_type.
145 typedef _Reference reference;
146 };
147
148 /**
149 * @brief Traits class for iterators.
150 *
151 * This class does nothing but define nested typedefs. The general
152 * version simply declares aliases for the nested typedefs from the Iterator
153 * argument. Partial specializations for pointers define the typedefs
154 * appropriately for the semantics of pointers.
155 */
156 template<typename _Iterator>
157 struct iterator_traits;
158
159#if __cplusplus >= 201103L
160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
161 // 2408. SFINAE-friendly common_type/iterator_traits is missing in C++14
162 template<typename _Iterator, typename = __void_t<>>
163 struct __iterator_traits { };
164
165#if ! __cpp_lib_concepts
166
167 template<typename _Iterator>
168 struct __iterator_traits<_Iterator,
169 __void_t<typename _Iterator::iterator_category,
170 typename _Iterator::value_type,
171 typename _Iterator::difference_type,
172 typename _Iterator::pointer,
173 typename _Iterator::reference>>
174 {
175 typedef typename _Iterator::iterator_category iterator_category;
176 typedef typename _Iterator::value_type value_type;
177 typedef typename _Iterator::difference_type difference_type;
178 typedef typename _Iterator::pointer pointer;
179 typedef typename _Iterator::reference reference;
180 };
181#endif // ! concepts
182
183 template<typename _Iterator>
185 : public __iterator_traits<_Iterator> { };
186
187#else // ! C++11
188 template<typename _Iterator>
189 struct iterator_traits
190 {
191 typedef typename _Iterator::iterator_category iterator_category;
192 typedef typename _Iterator::value_type value_type;
193 typedef typename _Iterator::difference_type difference_type;
194 typedef typename _Iterator::pointer pointer;
195 typedef typename _Iterator::reference reference;
196 };
197#endif // C++11
198
199#if __cplusplus > 201703L
200 /// Partial specialization for object pointer types.
201 template<typename _Tp>
202#if __cpp_concepts >= 201907L
203 requires is_object_v<_Tp>
204#endif
205 struct iterator_traits<_Tp*>
206 {
207 using iterator_concept = contiguous_iterator_tag;
208 using iterator_category = random_access_iterator_tag;
209 using value_type = remove_cv_t<_Tp>;
210 using difference_type = ptrdiff_t;
211 using pointer = _Tp*;
212 using reference = _Tp&;
213 };
214#else
215 /// Partial specialization for pointer types.
216 template<typename _Tp>
217 struct iterator_traits<_Tp*>
218 {
219 typedef random_access_iterator_tag iterator_category;
220 typedef _Tp value_type;
221 typedef ptrdiff_t difference_type;
222 typedef _Tp* pointer;
223 typedef _Tp& reference;
224 };
225
226 /// Partial specialization for const pointer types.
227 template<typename _Tp>
228 struct iterator_traits<const _Tp*>
229 {
230 typedef random_access_iterator_tag iterator_category;
231 typedef _Tp value_type;
232 typedef ptrdiff_t difference_type;
233 typedef const _Tp* pointer;
234 typedef const _Tp& reference;
235 };
236#endif
237
238 /// @cond undocumented
239 /**
240 * This function is not a part of the C++ standard but is syntactic
241 * sugar for internal library use only.
242 */
243 template<typename _Iter>
244 __attribute__((__always_inline__))
245 inline _GLIBCXX_CONSTEXPR
247 __iterator_category(const _Iter&)
248 { return typename iterator_traits<_Iter>::iterator_category(); }
249
250#if __cplusplus >= 201103L
251 template<typename _Iter>
252 using __iter_category_t
254
255 template<typename _InIter>
256 using _RequireInputIter =
257 __enable_if_t<is_convertible<__iter_category_t<_InIter>,
258 input_iterator_tag>::value>;
259
260#if __cpp_concepts
261 template<typename _InIter>
262 concept __has_input_iter_cat
263 = is_convertible_v<__iter_category_t<_InIter>, input_iterator_tag>;
264
265#ifdef __cpp_lib_concepts
266 // Is a Cpp17InputIterator or satisfies std::input_iterator.
267 template<typename _InIterator>
268 concept __any_input_iterator
269 = input_iterator<_InIterator> || __has_input_iter_cat<_InIterator>;
270
271 // Is a Cpp17ForwardIterator or satisfies std::forward_iterator or sized_sentinel_for
272 template<typename _InIterator>
273 concept __any_sizeable_iterator
274 = forward_iterator<_InIterator>
275 || is_convertible_v<__iter_category_t<_InIterator>, forward_iterator_tag>
276 || sized_sentinel_for<_InIterator, _InIterator>;
277#endif
278#endif
279
280 template<typename _It,
281 typename _Cat = __iter_category_t<_It>>
282 struct __is_random_access_iter
283 : is_base_of<random_access_iterator_tag, _Cat>
284 {
285 typedef is_base_of<random_access_iterator_tag, _Cat> _Base;
286 enum { __value = _Base::value };
287 };
288#else
289 template<typename _It, typename _Traits = iterator_traits<_It>,
290 typename _Cat = typename _Traits::iterator_category>
291 struct __is_random_access_iter
292 { enum { __value = __is_base_of(random_access_iterator_tag, _Cat) }; };
293#endif
294
295#pragma GCC diagnostic push
296#pragma GCC diagnostic ignored "-Wc++14-extensions" // variable templates
297 template<typename _Iter, typename = void>
298 const bool __enable_for_each_segment = false;
299
300 template<typename _Iter>
301 const bool __enable_for_each_segment<_Iter,
302#if __cplusplus >= 201103L
303 __enable_if_t<_Iter::_S_enable_for_each_segment>
304#else
305 typename __gnu_cxx::__enable_if<_Iter::_S_enable_for_each_segment, void>::__type
306#endif
307 > = true;
308#pragma GCC diagnostic pop
309
310#if __cpp_lib_concepts
311 template<typename _Iter>
312 concept __segmented_iterator = __enable_for_each_segment<_Iter>;
313#endif
314
315 /// @endcond
316 /// @}
317
318_GLIBCXX_END_NAMESPACE_VERSION
319} // namespace
320
321#if __glibcxx_algorithm_default_value_type // C++ >= 26
322# define _GLIBCXX26_DEF_VAL_T(T) = T
323# define _GLIBCXX26_ALGO_DEF_VAL_T(_Iterator) \
324 = typename iterator_traits<_Iterator>::value_type
325#else
326# define _GLIBCXX26_DEF_VAL_T(T)
327# define _GLIBCXX26_ALGO_DEF_VAL_T(_Iterator)
328#endif
329
330#endif /* _STL_ITERATOR_BASE_TYPES_H */
ISO C++ entities toplevel namespace is std.
is_base_of
Definition type_traits:1668
Traits class for iterators.
Marking input iterators.
Marking output iterators.
Forward iterators support a superset of input iterator operations.
Bidirectional iterators support a superset of forward iterator operations.
Random-access iterators support a superset of bidirectional iterator operations.
Contiguous iterators point to objects stored contiguously in memory.
Common iterator class.
_Category iterator_category
One of the tag types.
_Pointer pointer
This type represents a pointer-to-value_type.
_Distance difference_type
Distance between iterators is represented as this type.
_Reference reference
This type represents a reference-to-value_type.
_Tp value_type
The type "pointed to" by the iterator.