Boost logo

Boost :

Subject: Re: [boost] std::map::find() wrapper
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2011-05-04 03:37:01


On Tue, May 3, 2011 at 8:52 PM, Olaf van der Spek <ml_at_[hidden]> wrote:

> On Tue, May 3, 2011 at 6:57 PM, Robert Jones <robertgbjones_at_[hidden]>
> wrote:
> > I'd prefer to see it return a boost::optional<T&>
>
> What's the benefit?
>

Simply that the difficulty you're addressing is the possibility that there's
no
value to return. Boost.optional expresses that directly.

> > For containers of pointers to objects, the wrapper could do an
> >> additional dereference.
> >>
> >>
> > Not a good idea, it's easier to compose than decompose so return a
> > boost::optional<T*&> and the user deref if they want to.
>
> What problem would that avoid?
> It'd require the user to do an additional check.
>

But you'd lose some flexibility. If you return a reference or copy of the
pointed-to
object you cannot then change the pointer itself, whereas if you return a
reference to
the pointer you could.

It's also a special case, which is best avoided if possible, that doesn't
seem to offer
any real benefit.

- Rob.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk