GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::pointer_traits(3) C++ Standard Libary std::pointer_traits(3)

std::pointer_traits - std::pointer_traits


Defined in header <memory>
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.


1) The non-specialized pointer_traits declares the following types:

Member types


Type Definition
pointer Ptr
Ptr::element_type if present. Otherwise T if Ptr is a template
element_type specialization Template<T, Args...>. Otherwise, the pointer_traits
specialization is ill-formed
difference_type Ptr::difference_type if present, otherwise std::ptrdiff_t

Member alias templates


Template Definition
template <class U> using Ptr::rebind<U> if exists, otherwise Template<U, Args...> if
rebind Ptr is a template specialization Template<T, Args...>

Member functions


pointer_to obtains a dereferenceable pointer to its argument
[static] (public static member function)
Optional member functions of program-defined specializations
to_address obtains a raw pointer from a fancy pointer (inverse of
[static] (C++20)(optional) pointer_to)
(public static member function)


2) A specialization is provided for pointer types, T*, which declares the following
types:

Member types


Type Definition
pointer T*
element_type T
difference_type std::ptrdiff_t

Member alias templates


Template Definition
template< class U > using rebind U*

Member functions


pointer_to obtains a dereferenceable pointer to its argument
[static] (public static member function)


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,


using another_pointer = std::pointer_traits<std::shared_ptr<int>>::rebind<double>;
static_assert(std::is_same<another_pointer, std::shared_ptr<double>>::value);


A specialization for user-defined fancy pointer types may provide an
additional static member function to_address to customize the behavior (since C++20)
of std::to_address.


Feature-test macro: __cpp_lib_constexpr_memory

// Run this code


#include <memory>
#include <iostream>


template <class Ptr>
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<T>, block_ptr_t is smart_ptr<block>
using block_ptr_t = typename std::pointer_traits<Ptr>::template rebind<block>;


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<char>> bl2;
// The type of bl2.free_blocks is
// std::shared_ptr< BlockList<std::shared_ptr<char> >::block>
std::cout << bl2.free_blocks.use_count() << '\n';
}


0


allocator_traits provides information about allocator types
(C++11) (class template)
addressof obtains actual address of an object, even if the & operator is
(C++11) overloaded
(function template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.