Template Class not_null

Class Documentation

template<typename T>
class not_null

A pointer enforced to never be equivalent to nullptr.

Public Functions

template<typename = std::enable_if_t<!std::is_null_pointer<T>::value>>
inline constexpr not_null(T p)

Lvalue constructor.

Disabled if type T is std::nullptr_t.

Parameters

p – A non-null pointer.

Throws

std::invalid_argument – if p is equivalent to nullptr and the error mode is ErrorMode::DIE.

template<typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
inline constexpr not_null(U &&p)

Rvalue constructor.

Disabled if type U is not implicitly convertible to type T.

Parameters

p – Reference to a non-null pointer.

Throws

std::invalid_argument – if p references nullptr and the error mode is ErrorMode::DIE.

template<typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
inline constexpr not_null(const not_null<U> &other)

Custom copy constructor defined to use get() .

Disabled if type U of other is not implicitly convertible to type T.

Parameters

other – Object to copy.

not_null(const not_null &other) = default

Default copy constructor.

Parameters

other – Object to copy.

not_null(not_null &&other) = default

Default move constructor.

Parameters

other – Object to move.

not_null &operator=(const not_null &other) = default

Default copy assignment operator.

Parameters

other – Object to copy.

Returns

Reference to the newly copied object.

not_null &operator=(not_null &&other) = default

Default move assignment operator.

Parameters

other – Object to move.

Returns

Reference to the newly moved object.

inline constexpr std::conditional_t<std::is_copy_constructible<T>::value, T, const T&> get() const

Check and return the underlying pointer.

Throws

std::runtime_error – if underlying pointer is equivalent to nullptr and the error mode is ErrorMode::DIE.

Returns

The underlying pointer if it is still non-null. Returns a const reference if type T is not copy-constructible.

inline constexpr operator T() const

Define access operator to use get() .

Returns

Underlying pointer after null check.

inline constexpr AutoType operator->() const

Define member dereference operator to use get() .

Returns

Underlying pointer after null check.

inline constexpr AutoType operator*() const

Define dereference operator to use get() .

Returns

Dereferenced pointer after null check.

void operator[](std::ptrdiff_t) const = delete

Deleted.

not_null(std::nullptr_t) = delete

Deleted to prevent nullptr construction.

not_null &operator=(std::nullptr_t) = delete

Deleted to prevent nullptr assignment.

not_null &operator++() = delete

Deleted to prevent invalidation.

not_null &operator--() = delete

Deleted to prevent invalidation.

not_null operator++(int) = delete

Deleted to prevent invalidation.

not_null operator--(int) = delete

Deleted to prevent invalidation.

not_null &operator+=(std::ptrdiff_t) = delete

Deleted to prevent invalidation.

not_null &operator-=(std::ptrdiff_t) = delete

Deleted to prevent invalidation.

Friends

template<typename U, typename V>
friend std::shared_ptr<U> dynamic_pointer_cast(const navtk::not_null<V>&) noexcept

Declare friend overload for std::dynamic_pointer_cast for ease of use with not_null.