From time immemorial, when passing pointers to or from functions, we tend to special-case the null pointer:
p = get_pointer_to_the_foo(args);
if (p == nullptr) { /* foo is inaccessible, do one thing */ }
else { /* we can access foo, do something else */ }
and this is an inheritance from C. Now, we occasionally would do the same with other types, e.g. using a signed type to represent either a valid non-negative value, and, say, -1 or to indicate an error.
The latter pattern will now be finally deprecated with the onset of std::optional
: std::optional<unsigned>
is either nullopt
or a non-negative value. But - what about pointers? After all, nullptr
is just one of innumerable invalid pointer values. So, when writing new code (when all of it is C++17) - should we essentially forget about it, and pass around either std::optional<foo_t*>
's or assumed-non-null foo_t *
's?