C++ Logo

std-proposals

Advanced search

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

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Thu, 25 May 2023 12:29:35 +0200
On 25/05/2023 12:11, Frederick Virchanza Gotham via Std-Proposals wrote:
> 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
> 'container.end()' because the container can change in the meantime --
> and so I decided to use an std::optional< std::list<T>::iterator >.

By "the container" do you mean "the container type" or "the container
contents"?

If it's the former, if you actually use any container that yields
forward iterators, you can just use a value-initialized iterator object
as "null iterator" https://eel.is/c++draft/forward.iterators#2 . Just
static_assert that your iterator models forward_iterator.

If it's the latter, then there's nothing to worry about because
std::list never invalidates its end() iterator.

My 2 c,
-- 
Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

Received on 2023-05-25 10:29:38