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
70#endif
71
72#if __cplusplus > 201703L && __cpp_concepts >= 201907L
74#endif
75
76namespace std _GLIBCXX_VISIBILITY(default)
77{
78_GLIBCXX_BEGIN_NAMESPACE_VERSION
79
80 /**
81 * @defgroup iterators Iterators
82 * Abstractions for uniform iterating through various underlying types.
83 */
84 ///@{
85
86 /**
87 * @defgroup iterator_tags Iterator Tags
88 * These are empty types, used to distinguish different iterators. The
89 * distinction is not made by what they contain, but simply by what they
90 * are. Different underlying algorithms can then be used based on the
91 * different operations supported by different iterator types.
92 */
93 ///@{
94 /// Marking input iterators.
96
97 /// Marking output iterators.
99
100 /// Forward iterators support a superset of input iterator operations.
102
103 /// Bidirectional iterators support a superset of forward iterator
104 /// operations.
106
107 /// Random-access iterators support a superset of bidirectional
108 /// iterator operations.
110
111#if __cplusplus > 201703L
112 /// Contiguous iterators point to objects stored contiguously in memory.
114#endif
115 ///@}
116
117 /**
118 * @brief Common %iterator class.
119 *
120 * This class does nothing but define nested typedefs. %Iterator classes
121 * can inherit from this class to save some work. The typedefs are then
122 * used in specializations and overloading.
123 *
124 * In particular, there are no default implementations of requirements
125 * such as `operator++` and the like. (How could there be?)
126 *
127 * @deprecated Deprecated since C++17. The recommended alternative is to
128 * simply define the typedefs directly in your iterator class.
129 */
130 template<typename _Category, typename _Tp, typename _Distance = ptrdiff_t,
131 typename _Pointer = _Tp*, typename _Reference = _Tp&>
132 struct _GLIBCXX17_DEPRECATED iterator
133 {
134 /// One of the @link iterator_tags tag types@endlink.
135 typedef _Category iterator_category;
136 /// The type "pointed to" by the iterator.
137 typedef _Tp value_type;
138 /// Distance between iterators is represented as this type.
139 typedef _Distance difference_type;
140 /// This type represents a pointer-to-value_type.
141 typedef _Pointer pointer;
142 /// This type represents a reference-to-value_type.
143 typedef _Reference reference;
144 };
145
146 /**
147 * @brief Traits class for iterators.
148 *
149 * This class does nothing but define nested typedefs. The general
150 * version simply declares aliases for the nested typedefs from the Iterator
151 * argument. Partial specializations for pointers define the typedefs
152 * appropriately for the semantics of pointers.
153 */
154 template<typename _Iterator>
155 struct iterator_traits;
156
157#if __cplusplus >= 201103L
158 // _GLIBCXX_RESOLVE_LIB_DEFECTS
159 // 2408. SFINAE-friendly common_type/iterator_traits is missing in C++14
160 template<typename _Iterator, typename = __void_t<>>
161 struct __iterator_traits { };
162
163#if ! __cpp_lib_concepts
164
165 template<typename _Iterator>
166 struct __iterator_traits<_Iterator,
167 __void_t<typename _Iterator::iterator_category,
168 typename _Iterator::value_type,
169 typename _Iterator::difference_type,
170 typename _Iterator::pointer,
171 typename _Iterator::reference>>
172 {
173 typedef typename _Iterator::iterator_category iterator_category;
174 typedef typename _Iterator::value_type value_type;
175 typedef typename _Iterator::difference_type difference_type;
176 typedef typename _Iterator::pointer pointer;
177 typedef typename _Iterator::reference reference;
178 };
179#endif // ! concepts
180
181 template<typename _Iterator>
183 : public __iterator_traits<_Iterator> { };
184
185#else // ! C++11
186 template<typename _Iterator>
187 struct iterator_traits
188 {
189 typedef typename _Iterator::iterator_category iterator_category;
190 typedef typename _Iterator::value_type value_type;
191 typedef typename _Iterator::difference_type difference_type;
192 typedef typename _Iterator::pointer pointer;
193 typedef typename _Iterator::reference reference;
194 };
195#endif // C++11
196
197#if __cplusplus > 201703L
198 /// Partial specialization for object pointer types.
199 template<typename _Tp>
200#if __cpp_concepts >= 201907L
201 requires is_object_v<_Tp>
202#endif
203 struct iterator_traits<_Tp*>
204 {
205 using iterator_concept = contiguous_iterator_tag;
206 using iterator_category = random_access_iterator_tag;
207 using value_type = remove_cv_t<_Tp>;
208 using difference_type = ptrdiff_t;
209 using pointer = _Tp*;
210 using reference = _Tp&;
211 };
212#else
213 /// Partial specialization for pointer types.
214 template<typename _Tp>
215 struct iterator_traits<_Tp*>
216 {
217 typedef random_access_iterator_tag iterator_category;
218 typedef _Tp value_type;
219 typedef ptrdiff_t difference_type;
220 typedef _Tp* pointer;
221 typedef _Tp& reference;
222 };
223
224 /// Partial specialization for const pointer types.
225 template<typename _Tp>
226 struct iterator_traits<const _Tp*>
227 {
228 typedef random_access_iterator_tag iterator_category;
229 typedef _Tp value_type;
230 typedef ptrdiff_t difference_type;
231 typedef const _Tp* pointer;
232 typedef const _Tp& reference;
233 };
234#endif
235
236 /// @cond undocumented
237 /**
238 * This function is not a part of the C++ standard but is syntactic
239 * sugar for internal library use only.
240 */
241 template<typename _Iter>
242 __attribute__((__always_inline__))
243 inline _GLIBCXX_CONSTEXPR
245 __iterator_category(const _Iter&)
246 { return typename iterator_traits<_Iter>::iterator_category(); }
247
248#if __cplusplus >= 201103L
249 template<typename _Iter>
250 using __iter_category_t
252
253 template<typename _InIter>
254 using _RequireInputIter =
255 __enable_if_t<is_convertible<__iter_category_t<_InIter>,
256 input_iterator_tag>::value>;
257
258#if __cpp_concepts
259 template<typename _InIter>
260 concept __has_input_iter_cat
261 = is_convertible_v<__iter_category_t<_InIter>, input_iterator_tag>;
262
263#ifdef __cpp_lib_concepts
264 // Is a Cpp17InputIterator or satisfies std::input_iterator.
265 template<typename _InIterator>
266 concept __any_input_iterator
267 = input_iterator<_InIterator> || __has_input_iter_cat<_InIterator>;
268#endif
269#endif
270
271 template<typename _It,
272 typename _Cat = __iter_category_t<_It>>
273 struct __is_random_access_iter
274 : is_base_of<random_access_iterator_tag, _Cat>
275 {
276 typedef is_base_of<random_access_iterator_tag, _Cat> _Base;
277 enum { __value = _Base::value };
278 };
279#else
280 template<typename _It, typename _Traits = iterator_traits<_It>,
281 typename _Cat = typename _Traits::iterator_category>
282 struct __is_random_access_iter
283 { enum { __value = __is_base_of(random_access_iterator_tag, _Cat) }; };
284#endif
285
286 /// @endcond
287 /// @}
288
289_GLIBCXX_END_NAMESPACE_VERSION
290} // namespace
291
292#if __glibcxx_algorithm_default_value_type // C++ >= 26
293# define _GLIBCXX26_DEF_VAL_T(T) = T
294# define _GLIBCXX26_ALGO_DEF_VAL_T(_Iterator) \
295 = typename iterator_traits<_Iterator>::value_type
296#else
297# define _GLIBCXX26_DEF_VAL_T(T)
298# define _GLIBCXX26_ALGO_DEF_VAL_T(_Iterator)
299#endif
300
301#endif /* _STL_ITERATOR_BASE_TYPES_H */
ISO C++ entities toplevel namespace is std.
is_base_of
Definition type_traits:1643
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.