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