libstdc++
ptr_traits.h
Go to the documentation of this file.
1// Pointer Traits -*- C++ -*-
2
3// Copyright (C) 2011-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/** @file bits/ptr_traits.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
28 */
29
30#ifndef _PTR_TRAITS_H
31#define _PTR_TRAITS_H 1
32
33#if __cplusplus >= 201103L
34
35#include <bits/move.h>
36
37#if __cplusplus > 201703L
38#include <concepts>
39namespace __gnu_debug { class _Safe_iterator_base; }
40#endif
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 /// @cond undocumented
47
48 class __undefined;
49
50 // For a specialization `SomeTemplate<T, Types...>` the member `type` is T,
51 // otherwise `type` is `__undefined`.
52 template<typename _Tp>
53 struct __get_first_arg
54 { using type = __undefined; };
55
56 template<template<typename, typename...> class _SomeTemplate, typename _Tp,
57 typename... _Types>
58 struct __get_first_arg<_SomeTemplate<_Tp, _Types...>>
59 { using type = _Tp; };
60
61 // For a specialization `SomeTemplate<T, Args...>` and a type `U` the member
62 // `type` is `SomeTemplate<U, Args...>`, otherwise there is no member `type`.
63 template<typename _Tp, typename _Up>
64 struct __replace_first_arg
65 { };
66
67 template<template<typename, typename...> class _SomeTemplate, typename _Up,
68 typename _Tp, typename... _Types>
69 struct __replace_first_arg<_SomeTemplate<_Tp, _Types...>, _Up>
70 { using type = _SomeTemplate<_Up, _Types...>; };
71
72 // Detect the element type of a pointer-like type.
73 template<typename _Ptr, typename = void>
74 struct __ptr_traits_elem : __get_first_arg<_Ptr>
75 { };
76
77 // Use _Ptr::element_type if is a valid type.
78#if __cpp_concepts
79 template<typename _Ptr> requires requires { typename _Ptr::element_type; }
80 struct __ptr_traits_elem<_Ptr, void>
81 { using type = typename _Ptr::element_type; };
82#else
83 template<typename _Ptr>
84 struct __ptr_traits_elem<_Ptr, __void_t<typename _Ptr::element_type>>
85 { using type = typename _Ptr::element_type; };
86#endif
87
88 template<typename _Ptr>
89 using __ptr_traits_elem_t = typename __ptr_traits_elem<_Ptr>::type;
90
91 /// @endcond
92
93 // Define pointer_traits<P>::pointer_to.
94 template<typename _Ptr, typename _Elt, bool = is_void<_Elt>::value>
95 struct __ptr_traits_ptr_to
96 {
97 using pointer = _Ptr;
98 using element_type = _Elt;
99
100 // _GLIBCXX_RESOLVE_LIB_DEFECTS
101 // 3454. pointer_traits::pointer_to should be constexpr
102 /**
103 * @brief Obtain a pointer to an object
104 * @param __r A reference to an object of type `element_type`
105 * @return `pointer::pointer_to(__r)`
106 * @pre `pointer::pointer_to(__r)` is a valid expression.
107 */
108 static _GLIBCXX20_CONSTEXPR pointer
109 pointer_to(element_type& __r)
110#if __cpp_lib_concepts
111 requires requires {
112 { pointer::pointer_to(__r) } -> convertible_to<pointer>;
113 }
114#endif
115 { return pointer::pointer_to(__r); }
116 };
117
118 // Do not define pointer_traits<P>::pointer_to if element type is void.
119 template<typename _Ptr, typename _Elt>
120 struct __ptr_traits_ptr_to<_Ptr, _Elt, true>
121 { };
122
123 // Partial specialization defining pointer_traits<T*>::pointer_to(T&).
124 template<typename _Tp>
125 struct __ptr_traits_ptr_to<_Tp*, _Tp, false>
126 {
127 using pointer = _Tp*;
128 using element_type = _Tp;
129
130 /**
131 * @brief Obtain a pointer to an object
132 * @param __r A reference to an object of type `element_type`
133 * @return `addressof(__r)`
134 */
135 static _GLIBCXX20_CONSTEXPR pointer
136 pointer_to(element_type& __r) noexcept
137 { return std::addressof(__r); }
138 };
139
140 template<typename _Ptr, typename _Elt>
141 struct __ptr_traits_impl : __ptr_traits_ptr_to<_Ptr, _Elt>
142 {
143 private:
144 template<typename _Tp>
145 using __diff_t = typename _Tp::difference_type;
146
147 template<typename _Tp, typename _Up>
148 using __rebind = __type_identity<typename _Tp::template rebind<_Up>>;
149
150 public:
151 /// The pointer type.
152 using pointer = _Ptr;
153
154 /// The type pointed to.
155 using element_type = _Elt;
156
157 /// The type used to represent the difference between two pointers.
158 using difference_type = __detected_or_t<ptrdiff_t, __diff_t, _Ptr>;
159
160 /// A pointer to a different type.
161 template<typename _Up>
162 using rebind = typename __detected_or_t<__replace_first_arg<_Ptr, _Up>,
163 __rebind, _Ptr, _Up>::type;
164 };
165
166 // _GLIBCXX_RESOLVE_LIB_DEFECTS
167 // 3545. std::pointer_traits should be SFINAE-friendly
168 template<typename _Ptr>
169 struct __ptr_traits_impl<_Ptr, __undefined>
170 { };
171
172 /**
173 * @brief Uniform interface to all pointer-like types
174 * @headerfile memory
175 * @ingroup pointer_abstractions
176 * @since C++11
177 */
178 template<typename _Ptr>
179 struct pointer_traits : __ptr_traits_impl<_Ptr, __ptr_traits_elem_t<_Ptr>>
180 { };
181
182 /**
183 * @brief Partial specialization for built-in pointers.
184 * @headerfile memory
185 * @ingroup pointer_abstractions
186 * @since C++11
187 */
188 template<typename _Tp>
189 struct pointer_traits<_Tp*> : __ptr_traits_ptr_to<_Tp*, _Tp>
190 {
191 /// The pointer type
192 typedef _Tp* pointer;
193 /// The type pointed to
194 typedef _Tp element_type;
195 /// Type used to represent the difference between two pointers
196 typedef ptrdiff_t difference_type;
197 /// A pointer to a different type.
198 template<typename _Up> using rebind = _Up*;
199 };
200
201 /// Convenience alias for rebinding pointers.
202 template<typename _Ptr, typename _Tp>
203 using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>;
204
205#ifndef __glibcxx_to_address // C++ < 20
206 template<typename _Tp>
207 [[__gnu__::__always_inline__]]
208 constexpr _Tp*
209 __to_address(_Tp* __ptr) noexcept
210 {
211 static_assert(!std::is_function<_Tp>::value, "std::to_address argument "
212 "must not be a function pointer");
213 return __ptr;
214 }
215
216 // This should only be used for pointer-like types (e.g. allocator pointers)
217 // and (in C++20 and later) for types satisfying std::contiguous_iterator.
218 // It should not be used for arbitrary random access iterators, because
219 // they might not be contiguous iterators (e.g. deque::iterator isn't).
220 template<typename _Ptr>
221 constexpr typename std::pointer_traits<_Ptr>::element_type*
222 __to_address(const _Ptr& __ptr)
223 { return std::__to_address(__ptr.operator->()); }
224#else
225 /**
226 * @brief Obtain address referenced by a pointer to an object
227 * @param __ptr A pointer to an object
228 * @return `__ptr`
229 * @ingroup pointer_abstractions
230 */
231 template<typename _Tp>
232 [[__gnu__::__always_inline__]]
233 constexpr _Tp*
234 to_address(_Tp* __ptr) noexcept
235 {
236 static_assert(!is_function_v<_Tp>, "std::to_address argument "
237 "must not be a function pointer");
238 return __ptr;
239 }
240
241 /**
242 * @brief Obtain address referenced by a pointer to an object
243 * @param __ptr A pointer to an object
244 * @return `pointer_traits<_Ptr>::to_address(__ptr)` if that expression is
245 * well-formed, otherwise `to_address(__ptr.operator->())`.
246 * @ingroup pointer_abstractions
247 */
248 template<typename _Ptr>
249 constexpr auto
250 to_address(const _Ptr& __ptr) noexcept
251 {
252 if constexpr (requires { pointer_traits<_Ptr>::to_address(__ptr); })
254 else if constexpr (is_base_of_v<__gnu_debug::_Safe_iterator_base, _Ptr>)
255 return std::to_address(__ptr.base().operator->());
256 else
257 return std::to_address(__ptr.operator->());
258 }
259
260 /// @cond undocumented
261 /// Compatibility for use in code that is also compiled as pre-C++20.
262 template<typename _Ptr>
263 [[__gnu__::__always_inline__]]
264 constexpr auto
265 __to_address(const _Ptr& __ptr) noexcept
266 { return std::to_address(__ptr); }
267 /// @endcond
268#endif // __glibcxx_to_address
269
270_GLIBCXX_END_NAMESPACE_VERSION
271} // namespace std
272
273#endif
274
275#endif
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:234
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:176
ISO C++ entities toplevel namespace is std.
typename pointer_traits< _Ptr >::template rebind< _Tp > __ptr_rebind
Convenience alias for rebinding pointers.
Definition ptr_traits.h:203
GNU debug classes for public use.
is_function
Definition type_traits:742
Uniform interface to all pointer-like types.
Definition ptr_traits.h:180
_Tp * pointer
The pointer type.
Definition ptr_traits.h:192
ptrdiff_t difference_type
Type used to represent the difference between two pointers.
Definition ptr_traits.h:196
_Tp element_type
The type pointed to.
Definition ptr_traits.h:194
_Up * rebind
A pointer to a different type.
Definition ptr_traits.h:198
Basic functionality for a safe iterator.
Definition safe_base.h:51