Boost logo

Boost :

Subject: Re: [boost] std::map::find() wrapper
From: Olaf van der Spek (ml_at_[hidden])
Date: 2011-05-04 06:33:49


On Wed, May 4, 2011 at 9:37 AM, Robert Jones <robertgbjones_at_[hidden]> wrote:
> 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.

Returning NULL works just as well.
Using a raw pointer also allows you to write string* ptr = find_ptr().
Using Boost Optional would make that longer.

>> 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.

That's true, although I'm not sure that's a frequent use-case.

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

Containers of pointers occur quite frequently. There's even a Ptr Container lib.

Olaf


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