File: pointer_traits.qbk

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (175 lines) | stat: -rw-r--r-- 4,438 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
[/
Copyright 2017-2021 Glen Joseph Fernandes
(glenjofe@gmail.com)

Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
]

[section:pointer_traits pointer_traits]

[simplesect Authors]

* Glen Fernandes

[endsimplesect]

[section Overview]

The header <boost/core/pointer_traits.hpp> provides the class template
`boost::pointer_traits` to facilitate use of pointer-like types. The C++11
standard library introduced `std::pointer_traits` along with an allocator
model which supported pointer-like types in addition to plain raw pointers.
This implementation also supports C++98.

It also provides the function template `boost::to_address` to obtain a raw
pointer from an object of any pointer-like type.

[endsect]

[section Examples]

The following example allocates storage and constructs an object in that
storage using an allocator.

```
template<class Allocator>
void function(Allocator& a)
{
    auto p = a.allocate(1);
    std::allocator_traits<Allocator>::construct(a, boost::to_address(p));
}
```

[endsect]

[section Reference]

```
namespace boost {

template<class T>
struct pointer_traits {
    typedef T pointer;
    typedef ``['see below]`` element_type;
    typedef ``['see below]`` difference_type;

    template<class U>
    struct rebind_to {
        typedef ``['see below]`` type;
    };

    template<class U>
    using rebind = typename rebind_to<U>::type;

    static pointer pointer_to(element_type& v);
};

template<class T>
struct pointer_traits<T*> {
    typedef T* pointer;
    typedef T element_type;
    typedef std::ptrdiff_t difference_type;

    template<class U>
    struct rebind_to {
        typedef U* type;
    };

    template<class U>
    using rebind = typename rebind_to<U>::type;

    static pointer pointer_to(``['see below]`` v) noexcept;
};

template<class T>
constexpr T* to_address(T* v) noexcept;

template<class T>
auto to_address(const T& v) noexcept;

} // boost
```

[section Overview]

If the member type `element_type` is not defined, then all other members are
also not defined (`pointer_traits` is SFINAE-friendly).

[endsect]

[section Member types]

[variablelist
[[`typedef` ['see below] `element_type;`]
[`T::element_type` if such a type exists; otherwise `U` if `T` is a class
  template instantiation of the form `Pointer<U, Args>`, where `Args` is zero
  or more type arguments; otherwise the member is not defined.]]
[[`typedef` ['see below] `difference_type;`]
[`T::difference_type` if such a type exists; otherwise `std::ptrdiff_t`.]]
[[`template<class U> struct rebind_to { typedef` ['see below] `type; };`]
[`type` is `T::rebind<U>` if such a type exists; otherwise, `Pointer<V, Args>`
  if `T` is a class template instantiation of the form `Pointer<T, Args>`,
  where `Args` is zero or more type arguments; otherwise, the member is not
  defined.]]]

[note When C++11 template aliases are not supported, the `type` for `rebind` is
`T::rebind<U>::other` if such a type exists.]

[endsect]

[section Member functions]

[variablelist
[[`static pointer pointer_traits::pointer_to(element_type& v);`]
[[variablelist
[[Remark]
[If `element_type` is a void type, or if `T::pointer_to(v)` is not well-formed,
  this member is not defined.]]
[[Returns]
[A pointer to `v` obtained by calling `T::pointer_to(v)`.]]]]]
[[`static pointer pointer_traits<T*>::pointer_to(element_type& v) noexcept;`]
[[variablelist
[[Remark]
[If `element_type` is a void type, this member is not defined.]]
[[Returns][`addressof(v)`.]]]]]]

[endsect]

[section Optional members]

[variablelist
[[`static element_type* to_address(pointer v) noexcept;`]
[[variablelist
[[Returns]
[A pointer of type `element_type*` that references the same location as the
  argument `p`.]]
[[Note]
[This function should be the inverse of `pointer_to`. If defined, it
  customizes the behavior of the non-member function `to_address`.]]]]]]

[endsect]

[section Free functions]

[variablelist
[[`template<class T> constexpr T* to_address(T* v) noexcept;`]
[[variablelist
[[Returns][`v`.]]]]]
[[`template<class T> auto to_address(const T& v) noexcept;`]
[[variablelist
[[Returns][`pointer_traits<T>::to_address(v)` if that
  expression is well-formed, otherwise `to_address(v.operator->())`.]]]]]]

[endsect]

[endsect]

[section Acknowledgements]

Glen Fernandes implemented `pointer_traits` and `to_address` with reviews and
guidance from Peter Dimov.

[endsect]

[endsect]