Boost logo

Boost :

From: Gavin Lambert (boost_at_[hidden])
Date: 2022-04-04 00:46:28


Mere moments ago, quoth I:
> On 4/04/2022 10:58, Marshall Clow wrote:
>> On Apr 3, 2022, at 3:09 PM, Viktor Sehr wrote:
>>>
>>> Why not return a pointer/nullptr instead? pfind_if
>>
>> Why not just check to see if the returned iterator == end () ?
>
> The annoyance with the iterator == end() implementation is that it's a
> double indirection -- if you want to access the value afterwards it has
> to be in a separate statement, which means you can't do it with an
> rvalue collection.

To be clearer, it's just the need to access end() again that requires
putting the collection in a variable (assuming that you're using a
member or range algorithm, or something that otherwise doesn't already
require passing separate begin/end iterators).

> The optional return doesn't have that problem; because it copies the
> value, the value remains valid when returned from an rvalue collection.

And this applies only for optional<T>, not optional<T&> as was being
suggested. So a different case that's also not going to be valid for
rvalue collections.


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