C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Chaining of the -> operator for std::optional< std::list<T>::iterator >

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Thu, 25 May 2023 11:21:20 +0100
On Thu, 25 May 2023 at 11:11, Frederick Virchanza Gotham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Today I was re-working code that works with pointers to elements
> inside an 'std::list'. Then at one point I wanted to erase an element
> from the container, but to do so efficiently/properly, I needed an
> iterator rather than a pointer.
>
> So I refactored the code to use iterators instead of pointers. But of
> course, you can't have a null iterator -- and I don't want to use
>

For forward iterators you can use a value-initialized iterator as a null
iterator. Any valid iterator is guaranteed to compare non-equal with a
value-initialized iterator, and any two value-initialized iterators are
guaranteed to compare equal.

(I didn't bother reading the rest of your latest "I just thought of this
ten minutes ago so I'm sharing it" idea because it seems based on a
mistaken premise.)

Received on 2023-05-25 10:21:35