There are many places in the codebase where we use a pointer type (*, shared_ptr, unique_ptr, etc.) because of historical reasons (CBlockIndex*) or for polymorphism. In many such cases, we may want to avoid needing to handle the pointer being null, or make it clear in the function contract that we require a non-null pointer.
not_null<T> is the solution proposed by the C++ Core Guidelines (relevant section), and restricts pointers and smart pointers to hold non-null values.
Since I’ve learned about this, I’ve repeatedly ran into scenarios where I wish I could use it so as to make call semantics clearer and avoid misuse / unnecessary null handling.
There is a header-only implementation of not_null<T> here, however, there are two issues we need to solve:
- The header-only implementation linked above requires the
ExpectsandEnsuresmacros defined here, do we also import these or can we tweak our existing assertion macros to work? - There is also a
strict_not_null<T>, which requires explicit construction. Do we need that or justnot_null<T>?