Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

pointer_traits

Overview
Examples
Reference
Acknowledgments

Authors

  • Glen Fernandes

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, and adds additional functionality for obtaining raw pointers from pointers.

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));
}
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(see below v);
    static element_type* to_address(const pointer& v) noexcept;
  };

  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;
    static element_type* to_address(pointer v) noexcept;
  };

  template<class T>
  typename pointer_traits<T>::element_type* to_address(const T& v) noexcept;
}

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 specialization is ill-formed.

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 instantiation of rebind_to is ill-formed.

pointer_traits

static pointer pointer_to(see below v);

Remark

If element_type is a void type, the type of v is unspecified; otherwise, it is element_type&.

Returns

A pointer to v obtained by calling T::pointer_to(v).

static element_type* to_address(const pointer& v) noexcept;

Requires

v is not a null pointer.

Returns

A pointer of type element_type* that references the same location as the argument.

pointer_traits<T*>

static pointer pointer_to(see below v) noexcept;

Remark

If element_type is a void type, the type of v is unspecified; otherwise, it is element_type&.

Returns

The result of boost::addressof(v).

static element_type* to_address(pointer v) noexcept;

Returns

The value of v.

template<class T> typename pointer_traits<T>::element_type* to_address(const T& v) noexcept;

Returns

boost::pointer_traits<T>::to_address(v).

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


PrevUpHomeNext