Pointer traits and to_address
pointer_traits gives raw pointers and fancy pointers a uniform interface: how element_type, difference_type and rebind are derived from the template arguments, pointer_to and the optional to_address; std::to_address turns any pointer-like object back into a raw pointer without dereferencing it.
Key points
pointer_traits<P>answers what a pointer-like type points to (element_type), what subtracting two of them yields (difference_type), and what the same kind of pointer to another type is (rebind<U>).- When the type does not declare these members itself,
pointer_traitsby default takes the first template type argument aselement_type, andrebindreplaces that argument. - A type whose
element_typecannot be deduced gets an emptypointer_traits, not a hard error, so it can be used safely in arequiresexpression. std::to_address(p)gets a raw pointer throughoperator->orpointer_traits::to_addresswithout dereferencing, so it works onend()iterators too.
Fancy pointers#
C++ allocators allow allocator::pointer to be a class type rather than T*; this is called a fancy pointer (fancy pointer). The typical example is an offset pointer in shared memory: the same shared memory segment is mapped at different addresses in different processes, so storing an absolute address is meaningless, and only an offset relative to the pointer itself works. Boost.Interprocess's offset_ptr is such a pointer.
For a container to stay generic over such pointers, it has to answer several questions: what type does it point to? What type does subtracting two pointers yield? How do I get the same kind of pointer, but pointing to Node? How do I get a fancy pointer to an object from a reference to it? pointer_traits answers all of these uniformly, and [allocator.traits] also builds on it to derive const_pointer, void_pointer and difference_type.
#include <cassert>
#include <memory>
#include <type_traits>
#include <vector>
template<class T, class Tag = void>
struct Ptr { // a minimal fancy pointer
T* raw = nullptr;
T& operator*() const { return *raw; }
T* operator->() const { return raw; }
static Ptr pointer_to(T& r) { return Ptr{&r}; }
};
using PT = std::pointer_traits<Ptr<int, char>>;
static_assert(std::is_same_v<PT::element_type, int>); // the first template argument
static_assert(std::is_same_v<PT::difference_type, std::ptrdiff_t>); // the default
static_assert(std::is_same_v<PT::rebind<long>, Ptr<long, char>>); // replaces the first template argument
// a type that is not pointer-like gets an empty pointer_traits
template<class P> concept has_elem = requires { typename std::pointer_traits<P>::element_type; };
static_assert(!has_elem<int>);
static_assert(has_elem<int*>);
int main() {
int x = 42;
Ptr<int, char> p = PT::pointer_to(x);
assert(std::to_address(p) == &x); // falls back to p.operator->()
std::vector<int> v{1, 2, 3};
assert(std::to_address(v.begin()) == v.data()); // iterators work too
assert(std::to_address(v.end()) == v.data() + 3); // no dereference, so end() is safe
}Ptr declares no member types, yet pointer_traits still derives every answer. The rules follow one by one.
General [pointer.traits.general]#
¶1 pointer_traits provides a uniform interface to certain attributes of pointer-like types.
Raw pointers T* have a ready-made partial specialization: pointer is T*, element_type is T, difference_type is ptrdiff_t, rebind<U> is U*, and pointer_to(r) returns addressof(r). The members of the primary template have to be derived from Ptr.
How the member types are derived [pointer.traits.types]#
If deduction fails, the whole specialization is empty (¶1–¶2)#
¶1 defines the exposition-only class template ptr-traits-elem and the concept has-elem-type. ¶2 specifies that a specialization generated from the primary template has the members below only if Ptr satisfies has-elem-type; otherwise it has none of them.
This was established by LWG3545. Before that, pointer_traits<int> was a hard error on instantiation, and any code detecting whether a type is pointer-like would fail to compile because of it. Now pointer_traits<int> is just an empty class, so has_elem<int> in the example above truthfully yields false.
element_type (¶4)#
element_type is ptr-traits-elem<Ptr>::type, determined in this order:
Ptr::element_typeif it exists;- otherwise, if
Ptris a class template specialization of the formSomePointer<T, Args...>, the first template argumentT; - otherwise it cannot be deduced, and has-elem-type is not satisfied.
Rule 2 is a convention: it assumes that the first type argument of a fancy pointer is the pointed-to type, which holds for offset_ptr<T, ...> and for Ptr<T, Tag> in the example. It only applies to templates whose parameters are all types. Args in SomePointer<T, Args...> is a type pack, so a template with a non-type template parameter (such as my_ptr<T, 16>) does not match and has to declare element_type itself.
difference_type (¶5) and rebind (¶6)#
- ¶5
difference_type:Ptr::difference_typeif that is valid and denotes a type; otherwiseptrdiff_t. - ¶6
rebind<U>:Ptr::rebind<U>if that is valid and denotes a type; otherwise, ifPtrisSomePointer<T, Args...>,SomePointer<U, Args...>; otherwise instantiatingrebindis ill-formed.
"Valid and denotes a type" is judged by the rules of template argument deduction ([temp.deduct]), so even if Ptr::difference_type is a private member or not a type, the result just falls back to the default with no error.
#include <memory>
struct NotTemplate { using element_type = int; }; // not a template specialization, no rebind member
using R = std::pointer_traits<NotTemplate>::rebind<long>; // error: instantiating rebind is ill-formedlibc++ reports it with a static_assert: Cannot rebind pointer; did you forget to add a rebind member to your pointer?
pointer_to [pointer.traits.functions]#
pointer_to(r) gets a pointer to an object from a reference to it. A common use in container implementations: you hold a reference to a node object (such as a sentinel node that is a member of the container) and need its fancy pointer form to store in the list.
- ¶1 Mandates: for the primary template,
Ptr::pointer_to(r)is well-formed.pointer_traitsdoes not synthesize it for you. - ¶2 Preconditions: the pointer returned by
Ptr::pointer_to(r)really points torand can be used to access it. - ¶3 The primary template returns
Ptr::pointer_to(r); the raw pointer version returnsaddressof(r), not&r(seeaddressofin [memory]). - ¶4 If
element_typeis cvvoid, the type of the parameterris unspecified; otherwise it iselement_type&.voidhas no reference type, so there is no portable way to callpointer_traits<void*>::pointer_to.
LWG3454 made pointer_to constexpr.
Optional member to_address [pointer.traits.optmem]#
¶1 A specialization of pointer_traits may define a static member to_address(pointer p) to customize the library's behavior; specializations generated from the primary template have no such member. ¶2 It returns an element_type* pointing to the same location as p. The note in ¶3 says it is the inverse of pointer_to and, if defined, is used by the non-member std::to_address.
std::to_address [pointer.conversion]#
The most direct way to turn any pointer-like object back into a raw pointer is &*p. It has two problems: dereferencing an end() iterator or a null fancy pointer is undefined behavior, and operator* and operator& may both be overloaded to mean something else. C++20's std::to_address (P0653R2) gets the address without dereferencing:
- ¶1–¶2 For a raw pointer
T*, it returnspdirectly; the Mandates requiresTnot to be a function type, soto_address(&f)is ill-formed. - ¶3 For other types, it uses
pointer_traits<Ptr>::to_address(p)if that is well-formed, and otherwise returnsto_address(p.operator->()). This is a recursive call, so it is fine foroperator->to return another fancy pointer.
Because it goes through operator-> rather than operator*, to_address(it) gets the element address for any contiguous iterator of the standard library, including end(). The contiguous_iterator concept ([iterator.concept.contiguous]) uses exactly to_address to state the correspondence between iterators and element addresses.
to_address does not make an invalidated iterator valid
to_address itself does not dereference, but any operation on an iterator that has already been invalidated (including calling operator->) is still undefined behavior.
Questions#
Question 1
template<class T, int N> struct aligned_ptr { T* p; }; declares no member types. What is pointer_traits<aligned_ptr<int, 16>>::element_type?
Answer
It does not exist. The second partial specialization of ptr-traits-elem requires the form SomePointer<T, Args...>, where Args... is a pack of type template arguments; the second template parameter of aligned_ptr is a non-type parameter, so matching fails. has-elem-type is then not satisfied, and by ¶2 the whole specialization has no members at all. The fix is to write using element_type = T; and template<class U> using rebind = aligned_ptr<U, N>; in aligned_ptr.
Question 2
Why does std::to_address prefer pointer_traits::to_address over always using operator->?
Answer
For some pointer-like types, operator-> may be checked when the value is valid but not dereferenceable; for example, a debug-mode iterator asserts when operator-> is called on end(). Some types have no operator-> at all. Specializing pointer_traits with a to_address gives such a type a way to compute the address only, without any access checks. libc++ does exactly this: it specializes pointer_traits for __bounded_iter, its bounds-checked iterator in hardened mode, providing a to_address that does no bounds checking.
Summary#
| Member | Default source | When it cannot be derived |
|---|---|---|
element_type |
Ptr::element_type, otherwise the first template type argument |
The whole specialization has no members |
difference_type |
Ptr::difference_type, otherwise ptrdiff_t |
Never fails |
rebind<U> |
Ptr::rebind<U>, otherwise replace the first template argument |
Instantiation is ill-formed |
pointer_to(r) |
Must be provided by Ptr::pointer_to |
Ill-formed (Mandates) |
to_address(p) |
Optional, only an explicit specialization can provide it | std::to_address falls back to operator-> |