piped::extra::ptr

<piped/ptr.hpp>

template<typename T>
class piped::extra::ptr;

A transparent pointer wrapper. This functions as an option_type, i.e. implements unary *, and is bool-convertible.

This is nice for three primary reasons:

  • Simple, debug-optional, cross-platform nullptr-check
  • Default nullptr initialization!
  • All of ptr<T>, const ptr<T>, ptr<const T>, and const ptr<const T> act exactly as you'd expect.

You may be asking, "why check for nullptr? Won't it just crash anyway?" Note the following will probably not crash:

class C {
  void foo() {
     // Do things not referencing instance data members
  }

};

C *c = nullptr;
c->foo();

This may lead to unobvious errors elsewhere if you're not checking this pointers in your debugger. ptr<T>, with debugging enabled, will cause an abort at the point you do c->foo().

Name Description
ptr()=default Default-initialize to nullptr
ptr(T *p) Default convert from T*a.
ptr(const ptr &p)=default Copy.
ptr(const ptr< S > &p) Handle ptr<T> to ptr<const T>
operator *() const Return a reference.
operator *() Non-const.
operator->() const Dereference.
operator->() Non-const.
operator bool() true if pointer is not nullptr.
is_null() Non-operator, affirmative check for nullptr.
get() const Non-cast-operator conversion to pointer.
get() Non-const.
operator const T *() const Cast operator to T*.
operator T *() Non-const.
operator=(const ptr &p) Assign from const ptr<T>. T's const-ness is orthogonal.

Members

ptr()=default

<piped/ptr.hpp>

piped::extra::ptr< T >::ptr()=default

Description

Default-initialize to nullptr

ptr(T *p)

<piped/ptr.hpp>

piped::extra::ptr< T >::ptr(T *p)

Description

Default convert from T*a.

Parameter
p T *

ptr(const ptr &p)=default

<piped/ptr.hpp>

piped::extra::ptr< T >::ptr(const ptr &p)=default

Description

Copy.

Parameter
p [object Object]

<S,> ptr(const ptr< S > &p)

<piped/ptr.hpp>

template<typename S undefined, typename undefined>
piped::extra::ptr< T >::ptr(const ptr< S > &p)

Description

Handle ptr<T> to ptr<const T>

Parameter
p [object Object]

operator *() const

<piped/ptr.hpp>

T& piped::extra::ptr< T >::operator *() const

Description

Return a reference.

operator *()

<piped/ptr.hpp>

T& piped::extra::ptr< T >::operator *()

Description

Non-const.

operator->() const

<piped/ptr.hpp>

T* piped::extra::ptr< T >::operator->() const

Description

Dereference.

operator->()

<piped/ptr.hpp>

T* piped::extra::ptr< T >::operator->()

Description

Non-const.

operator bool()

<piped/ptr.hpp>

piped::extra::ptr< T >::operator bool()

Description

true if pointer is not nullptr.

is_null()

<piped/ptr.hpp>

bool piped::extra::ptr< T >::is_null()

Description

Non-operator, affirmative check for nullptr.

get() const

<piped/ptr.hpp>

T* piped::extra::ptr< T >::get() const

Description

Non-cast-operator conversion to pointer.

get()

<piped/ptr.hpp>

T* piped::extra::ptr< T >::get()

Description

Non-const.

operator const T *() const

<piped/ptr.hpp>

piped::extra::ptr< T >::operator const T *() const

Description

Cast operator to T*.

operator T *()

<piped/ptr.hpp>

piped::extra::ptr< T >::operator T *()

Description

Non-const.

operator=(const ptr &p)

<piped/ptr.hpp>

ptr& piped::extra::ptr< T >::operator=(const ptr &p)

Description

Assign from const ptr<T>. T's const-ness is orthogonal.

Parameter
p [object Object]