From: Ross MacGregor (gordonrossmacgregor_at_[hidden])
Date: 2008-04-02 12:58:29
I just created a very small smart pointer libary for our project. It will
contain two types of shared pointers: SharedHandle and NullableSharedHandle.
SharedHandle will be a shared pointer that cannot be null by ensuring a
non-null construction while NullableSharedHandle will basically be a typedef
I firmly believe that code can be made more reliable by minimizing the use
of nullable pointers. To this end, I think it is important that we start
using non-null pointer types. Ideally the compiler would be able to
understand a non-null pointer and catch bugs at compile time, but I think we
can still improve things with a few runtime checks. If we make sure that a
shared pointer is constructed with a non-null pointer and ban any reset()
type methods, we guarentee that it points to something. This can be
beneficial for the following reasons:
1. Any function that uses this type will no longer be burdened with
precondition checks to make sure variables of this type are not null.
2. It's type is simply more explicit and better explains its function.
3. Moving the non-null check to the creation phase will reduce the
possibility of encountering null pointer exceptions at runtime (assuming
there are many more pointer references than constructor calls).
So my little pointer library seems to make a lot of sense, but if so why
hasn't this simple smart pointer variation made it into boost yet? Has this
been discussed somewhere already? My search on non-null smart pointers is
not revealing much discussion on the topic.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk