C++ Logo

std-proposals

Advanced search

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

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Thu, 25 May 2023 08:26:39 -0500
On Thu, 25 May 2023 at 05:21, Jonathan Wakely via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> 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 don't think that's right; my reading of [forward.iterators]/2 is that
each instance of a container defines a comparison domain, and singular
iterators are in their own distinct domain.

btw, _GLIBCXX_DEBUG agrees with me, and IIRC MSVC-STL _ITERATOR_DEBUG_LEVEL
as well: https://godbolt.org/z/nEG8Gaosq

#include <list>
int main() {
    return std::list<int>().begin() == std::list<int>::iterator();
}
Error: attempt to compare a past-the-end iterator to a
singular (value-initialized) iterator.

Received on 2023-05-25 13:26:52