Bitcoin Core 32.99.0
P2P Digital Currency
Public Member Functions | Private Attributes | List of all members
util::NotNull< T > Class Template Reference

NotNull is intended to be used to denote that a smart pointer type can not hold a nullptr value. More...

#include <not_null.h>

Collaboration diagram for util::NotNull< T >:
[legend]

Public Member Functions

template<std::convertible_to< T > U>
requires (!std::same_as<std::remove_cvref_t<U>, NotNull>)
constexpr NotNull (U &&u) noexcept(std::is_nothrow_constructible_v< T, U && >)
 
template<class U >
requires std::move_constructible<U> && std::constructible_from<T, U&&>
constexpr explicit (!std::convertible_to< U, T >) NotNull(NotNull< U > &&other) noexcept(std::is_nothrow_move_constructible_v< U > &&std::is_nothrow_constructible_v< T, U && >)
 
template<class U >
requires std::convertible_to<const U&, T>
constexpr NotNull (const NotNull< U > &other) noexcept(std::is_nothrow_constructible_v< T, const U & >)
 
 NotNull (std::nullptr_t)=delete
 
NotNull & operator= (std::nullptr_t)=delete
 
constexpr NotNull (NotNull &&)=default
 
constexpr NotNull & operator= (NotNull &&)=default
 
constexpr NotNull (const NotNull &)=default
 
constexpr NotNull & operator= (const NotNull &)=default
 
constexpr const T & get () const &noexcept LIFETIMEBOUND
 
constexpr T get () &&noexcept(std::is_nothrow_move_constructible_v< T >)
 
constexpr decltype(auto) operator-> () const LIFETIMEBOUND
 
constexpr decltype(auto) operator* () const LIFETIMEBOUND
 
constexpr operator T () const &noexcept(std::is_nothrow_copy_constructible_v< T >)
 
constexpr operator T () &&noexcept(std::is_nothrow_move_constructible_v< T >)
 
template<class U >
constexpr auto operator<=> (const NotNull< U > &other) const
 
template<class U >
constexpr bool operator== (const NotNull< U > &other) const
 

Private Attributes

T m_ptr
 

Detailed Description

template<class T>
requires std::is_class_v<T> && requires(T t) { { t != nullptr } -> std::convertible_to<bool>; }
class util::NotNull< T >

NotNull is intended to be used to denote that a smart pointer type can not hold a nullptr value.

The type is more useful, the earlier in the lifetime of a pointer it is used. So instead of passing an existing pointer into NotNull, it is recommended to use the type when creating the smart pointer. E.g.:

util::NotNull p{std::make_shared<Thing>()};

This makes it obvious that a pointer really is not null and that the constructor will not fail.

NotNull does not support raw pointers. The C++ language provides raw references for this use case, and the C++ standard library provides std::reference_wrapper, where raw references can not be used.

This type is movable, meaning that the inner pointer is null after a move. Clang-tidy static analysis is used to enforce use-after-move violations, so that the null should never be observed. Moreover, the runtime Assume check in the get() member function will ensure the same.

Definition at line 44 of file not_null.h.

Constructor & Destructor Documentation

◆ NotNull() [1/5]

template<class T >
template<std::convertible_to< T > U>
requires (!std::same_as<std::remove_cvref_t<U>, NotNull>)
constexpr util::NotNull< T >::NotNull ( U &&  u)
inlineexplicitconstexprnoexcept

Definition at line 49 of file not_null.h.

◆ NotNull() [2/5]

template<class T >
template<class U >
requires std::convertible_to<const U&, T>
constexpr util::NotNull< T >::NotNull ( const NotNull< U > &  other)
inlineconstexprnoexcept

Definition at line 66 of file not_null.h.

◆ NotNull() [3/5]

template<class T >
util::NotNull< T >::NotNull ( std::nullptr_t  )
delete

◆ NotNull() [4/5]

template<class T >
constexpr util::NotNull< T >::NotNull ( NotNull< T > &&  )
constexprdefault

◆ NotNull() [5/5]

template<class T >
constexpr util::NotNull< T >::NotNull ( const NotNull< T > &  )
constexprdefault

Member Function Documentation

◆ explicit()

template<class T >
template<class U >
requires std::move_constructible<U> && std::constructible_from<T, U&&>
constexpr util::NotNull< T >::explicit ( !std::convertible_to< U, T >  ) &&
inlineconstexprnoexcept

Definition at line 57 of file not_null.h.

◆ get() [1/2]

template<class T >
constexpr T util::NotNull< T >::get ( ) &&
inlineconstexprnoexcept

Definition at line 81 of file not_null.h.

◆ get() [2/2]

template<class T >
constexpr const T & util::NotNull< T >::get ( ) const &
inlineconstexprnoexcept

Definition at line 80 of file not_null.h.

Here is the caller graph for this function:

◆ operator T() [1/2]

template<class T >
constexpr util::NotNull< T >::operator T ( ) &&
inlineconstexprnoexcept

Definition at line 95 of file not_null.h.

◆ operator T() [2/2]

template<class T >
constexpr util::NotNull< T >::operator T ( ) const &
inlineconstexprnoexcept

Definition at line 90 of file not_null.h.

◆ operator*()

template<class T >
constexpr decltype(auto) util::NotNull< T >::operator* ( ) const
inlineconstexpr

Definition at line 88 of file not_null.h.

Here is the call graph for this function:

◆ operator->()

template<class T >
constexpr decltype(auto) util::NotNull< T >::operator-> ( ) const
inlineconstexpr

Definition at line 87 of file not_null.h.

Here is the call graph for this function:

◆ operator<=>()

template<class T >
template<class U >
constexpr auto util::NotNull< T >::operator<=> ( const NotNull< U > &  other) const
inlineconstexpr

Definition at line 102 of file not_null.h.

Here is the call graph for this function:

◆ operator=() [1/3]

template<class T >
constexpr NotNull & util::NotNull< T >::operator= ( const NotNull< T > &  )
constexprdefault

◆ operator=() [2/3]

template<class T >
constexpr NotNull & util::NotNull< T >::operator= ( NotNull< T > &&  )
constexprdefault

◆ operator=() [3/3]

template<class T >
NotNull & util::NotNull< T >::operator= ( std::nullptr_t  )
delete

◆ operator==()

template<class T >
template<class U >
constexpr bool util::NotNull< T >::operator== ( const NotNull< U > &  other) const
inlineconstexpr

Definition at line 107 of file not_null.h.

Here is the call graph for this function:

Member Data Documentation

◆ m_ptr

template<class T >
T util::NotNull< T >::m_ptr
private

Definition at line 113 of file not_null.h.


The documentation for this class was generated from the following file: