After adding the comment "// not null"
to a raw pointer for the Nth time I wondered once again whatever happened to the not_null
template.
The C++ core guidelines were created quite some time ago now and a few things have made into into the standard including for example std::span
(some like string_view
and std::array
originated before the core guidelines themselves but are sometimes conflated). Given its relative simplicity why hasn't not_null (or anything similar) made it into the standard yet?
I scan the ISO mailings regularly (but perhaps not thoroughly) and I am not even aware of a proposal for it.
Possibly answering my own question. I do not recall coming across any cases where it would have prevented a bug in code I've worked on as we try not to write code that way.
The guidelines themselves are quite popular, making it into clang-tidy and sonar for example. The support libraries seem a little less popular.
For example boost has been available as a package on Linux from near the start. I am not aware of any implementation of GSL that is. Though, I presume it is bundled with Visual C++ on windows.
Since people have asked in the comments.
Myself I would use it to document intent.
A construct like not_null<>
potentially has semantic value which a comment does not.
Enforcing it is secondary though I can see its place. This would preferably be done with zero overhead (perhaps at compile time only for a limited number of cases).
I was thinking mainly about the case of a raw pointer member variable. I had forgotten about the case of passing a pointer to a function for which I always use a reference to mean not-null and also to mean "I am not taking ownership".
Likewise (for class members) we could also document ownership owned<>
not_owned<>
.
I suppose there is also whether the associated object is allowed to be changed. This might be too high level though. You could use references members instead of pointers to document this. I avoid reference members myself because I almost always want copyable and assignable types. However, see for example Should I prefer pointers or references in member data? for some discussion of this.
Another dimension is whether another entity can modify a variable. "const" says I promise not to modify it. In multi-threaded code we would like to say almost the opposite. That is "other code promises not to modify it while we are using it" (without an explicit lock) but this is way off topic...
There is one big technical issue that is likely unsolvable which makes standardizing not_null
a problem: it cannot work with move-only smart pointers.
The most important use case for not_null
is with smart pointers (for raw pointers a reference usually is adequate, but even then, there are times when a reference won't work). not_null<shared_ptr<T>>
is a useful thing that says something important about the API that consumes such an object.
But not_null<unique_ptr<T>>
doesn't work. It cannot work. The reason being that moving from a unique pointer leaves the old object null. Which is exactly what not_null
is expected to prevent. Therefore, not_null<T>
always forces a copy on its contained T
. Which... you can't do with a unique_ptr
, because that's the whole point of the type.
Being able to say that the unqiue_ptr
consumed by an API is not null is good and useful. But you can't actually do that with not_null
, which puts a hole in its utility.
So long as move-only smart pointers can't work with not_null
, standardizing the class becomes problematic.