std::pointer_traits - cppreference.com (original) (raw)
Defined in header | ||
---|---|---|
template< class Ptr > struct pointer_traits; | (1) | (since C++11) |
template< class T > struct pointer_traits<T*>; | (2) | (since C++11) |
The pointer_traits
class template provides the standardized way to access certain properties of pointer-like types (fancy pointers, such as boost::interprocess::offset_ptr). The standard template std::allocator_traits relies on pointer_traits
to determine the defaults for various typedefs required by Allocator.
The non-specialized
pointer_traits
conditionally declares the following members:A specialization is provided for pointer types, T*, which declares the following members:
[edit] Member types
Type | Definition |
---|---|
pointer | T* |
element_type | T |
difference_type | std::ptrdiff_t |
[edit] Member alias templates
Template | Definition |
---|---|
template< class U > using rebind | U* |
[edit] Member functions
| | obtains a dereferenceable pointer to its argument (public static member function) [edit] | | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
[edit] Optional member functions of program-defined specializations
[static] (C++20)(optional) | obtains a raw pointer from a fancy pointer (inverse of pointer_to) (public static member function) [edit] |
---|
[edit] Notes
The rebind member template alias makes it possible, given a pointer-like type that points to T
, to obtain the same pointer-like type that points to U
. For example,
A specialization for user-defined fancy pointer types may provide an additional static member function to_address to customize the behavior of std::to_address. | (since C++20) |
---|
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_constexpr_memory | 201811L | (C++20) | constexpr in std::pointer_traits |
[edit] Example
#include #include template struct BlockList { // Predefine a memory block struct block; // Define a pointer to a memory block from the kind of pointer Ptr s // If Ptr is any kind of T*, block_ptr_t is block* // If Ptr is smart_ptr, block_ptr_t is smart_ptr using block_ptr_t = typename std::pointer_traits::template rebind; struct block { std::size_t size{}; block_ptr_t next_block{}; }; block_ptr_t free_blocks; }; int main() { [[maybe_unused]] BlockList<int*> bl1; // The type of bl1.free_blocks is BlockList<int*>:: block* BlockList<std::shared_ptr> bl2; // The type of bl2.free_blocks is // std::shared_ptr<BlockList<std::shared_ptr>::block> std::cout << bl2.free_blocks.use_count() << '\n'; }
Output:
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3545 | C++11 | primary template caused hard error when element_type is invalid | made SFINAE-friendly |
[edit] See also
| | provides information about allocator types (class template) [edit] | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | | obtains actual address of an object, even if the & operator is overloaded (function template) [edit] |