[pointer.traits] # 20 Memory management library [[mem]](./#mem) ## 20.2 Memory [[memory]](memory#pointer.traits) ### 20.2.3 Pointer traits [pointer.traits] #### [20.2.3.1](#general) General [[pointer.traits.general]](pointer.traits.general) [1](#general-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L745) The class template pointer_traits supplies a uniform interface to certain attributes of pointer-like types[.](#general-1.sentence-1) [🔗](#lib:pointer_traits) namespace std {template struct pointer_traits {*see below*; }; template struct pointer_traits {using pointer = T*; using element_type = T; using difference_type = ptrdiff_t; template using rebind = U*; static constexpr pointer pointer_to(*see below* r) noexcept; };} #### [20.2.3.2](#types) Member types [[pointer.traits.types]](pointer.traits.types) [1](#types-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L770) The definitions in this subclause make use of the following exposition-only class template and concept:templatestruct *ptr-traits-elem* // *exposition only*{ }; template requires requires { typename T::element_type; }struct *ptr-traits-elem*{ using type = typename T::element_type; }; template class SomePointer, class T, class... Args>requires (!requires { typename SomePointer::element_type; })struct *ptr-traits-elem*>{ using type = T; }; templateconcept [*has-elem-type*](#concept:has-elem-type "20.2.3.2 Member types [pointer.traits.types]") = // *exposition only*requires { typename *ptr-traits-elem*::type; } [2](#types-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L792) If Ptr satisfies [*has-elem-type*](#concept:has-elem-type "20.2.3.2 Member types [pointer.traits.types]"), a specialization pointer_traits generated from the pointer_traits primary template has the following members as well as those described in [[pointer.traits.functions]](#functions "20.2.3.3 Member functions"); otherwise, such a specialization has no members by any of those names[.](#types-2.sentence-1) [🔗](#lib:pointer,pointer_traits) `using pointer = see below; ` [3](#types-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L806) *Type*: Ptr[.](#types-3.sentence-1) [🔗](#lib:element_type,pointer_traits) `using element_type = see below; ` [4](#types-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L816) *Type*: typename *ptr-traits-elem*​::​type[.](#types-4.sentence-1) [🔗](#lib:difference_type,pointer_traits) `using difference_type = see below; ` [5](#types-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L826) *Type*: Ptr​::​difference_type if the [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3 Qualified names [expr.prim.id.qual]") Ptr​::​difference_type is valid and denotes a type ([[temp.deduct]](temp.deduct "13.10.3 Template argument deduction")); otherwise,ptrdiff_t[.](#types-5.sentence-1) [🔗](#lib:rebind,pointer_traits) `template using rebind = see below; ` [6](#types-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L839) *Alias template*: Ptr​::​rebind if the [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3 Qualified names [expr.prim.id.qual]") Ptr​::​rebind is valid and denotes a type ([[temp.deduct]](temp.deduct "13.10.3 Template argument deduction")); otherwise,SomePointer ifPtr is a class template instantiation of the form SomePointer, where Args is zero or more type arguments; otherwise, the instantiation ofrebind is ill-formed[.](#types-6.sentence-1) #### [20.2.3.3](#functions) Member functions [[pointer.traits.functions]](pointer.traits.functions) [🔗](#lib:pointer_to,pointer_traits) `static pointer pointer_traits::pointer_to(see below r); static constexpr pointer pointer_traits::pointer_to(see below r) noexcept; ` [1](#functions-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L858) *Mandates*: For the first member function,Ptr​::​pointer_to(r) is well-formed[.](#functions-1.sentence-1) [2](#functions-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L863) *Preconditions*: For the first member function,Ptr​::​pointer_to(r) returns a pointer to r through which indirection is valid[.](#functions-2.sentence-1) [3](#functions-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L869) *Returns*: The first member function returns Ptr​::​pointer_to(r)[.](#functions-3.sentence-1) The second member function returns addressof(r)[.](#functions-3.sentence-2) [4](#functions-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L874) *Remarks*: If element_type is cv void, the type ofr is unspecified; otherwise, it is element_type&[.](#functions-4.sentence-1) #### [20.2.3.4](#optmem) Optional members [[pointer.traits.optmem]](pointer.traits.optmem) [1](#optmem-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L882) Specializations of pointer_traits may define the member declared in this subclause to customize the behavior of the standard library[.](#optmem-1.sentence-1) A specialization generated from the pointer_traits primary template has no member by this name[.](#optmem-1.sentence-2) [🔗](#lib:to_address,pointer_traits) `static element_type* to_address(pointer p) noexcept; ` [2](#optmem-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L894) *Returns*: A pointer of type element_type* that references the same location as the argument p[.](#optmem-2.sentence-1) [3](#optmem-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L899) [*Note [1](#optmem-note-1)*: This function is intended to be the inverse of pointer_to[.](#optmem-3.sentence-1) If defined, it customizes the behavior of the non-member functionto_address ([[pointer.conversion]](pointer.conversion "20.2.4 Pointer conversion"))[.](#optmem-3.sentence-2) — *end note*]