Boost logo

Boost Users :

Subject: Re: [Boost-users] non nullable smart pointers
From: Brendan Miller (catphive_at_[hidden])
Date: 2008-11-15 15:18:28

On Sat, Nov 15, 2008 at 2:49 AM, Thorsten Ottosen
<thorsten.ottosen_at_[hidden]> wrote:
> Brendan Miller skrev:
>> I've been interested in non nullable smart pointers for a while. I
>> noticed a thread I wanted to check if anyone was still interested in
>> doing it:
>> This seems like a worthwhile activity. Null is a legacy element from C
>> that essentially breaks type safety.
> FWIW, Boost.PtrContainer does not allow you to put nulls in containers; if
> you want nulls you have to ask for it:
> boost::ptr_vector< boost::nullable<T> > vec;

I'm always shocked how many good ideas have already been implemented in boost.

> I guess it would be trivial to add something like
> typedef boost::shared_ptr< boost::non_null<T> > ptr_type;
> which would not be default constructible, and which would throw if you
> inserted a null (rather than providing a check in operator->).

You're suggesting that there would be a specialization of shared_ptr
for shared_ptr<boost::non_null<T>>? That's a good idea, but I have no
idea on whether the maintainers of shared_ptr are willing to accept
that much more complexity. Also, shared_ptr<non_null<t>> is really a
mouthful, but I guess when c++0x comes out you could just do a
template using to shared_ref<t> or something for those (such as
myself) who want to consistently prohibit null in their code to
promote type safety.

Really though, if you had a non-nullable reference counted pointer,
you'd also want a non-nullable unmanaged pointer, a non-nullable weak
pointer, a non-nullable scoped pointer and appropriate conversions,
i.e. you can assign a non-null unmanaged pointer to a non-null
reference counted pointer and vice versa without a runtime null check
being done.

That's why I was thinking of just rolling a separate set of pointers,
maybe taking shared_ptr and scoped_ptr as starting points. I don't
know who the maintainer is, but i wasn't sure whether they would
accept a patch with that much added complexity.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at