C++ Logo


Advanced search

Re: [std-proposals] Helper type

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 23 Nov 2022 19:24:24 -0500
On Wed, Nov 23, 2022 at 7:02 PM organicoman via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> Hello gents,
> I would like to suggest the following helper type:
> "null_iter"
> It is of type iterator but convertible to the boolean value "false", much like the type: "nullptr"
> This value "null_iter" is returned by all container iterators types (vector::iterator, list::iterator, set::iterator...etc) to denote the end of the container when you apply the increment or decrement operators.
> For exple:
> --------- code snippet -------
> std::vector<int> v{1,2,3,4};
> auto iter = v.begin();
> while(++iter)
> {
> //
> }
> ------- end of snippet------
> When "iter" hits "v.end()", it will return "null_iter" which will be converted to "false" then the loop stops.
> What do you think?

This is a bad idea.

Many iterators do not know when they are at the end; iterators are
frequently very dumb. Pointers are iterators into contiguous arrays,
so there's no way to even implement "null_iter". Indeed, the *entire
point* of the iterator model is that you need a second object to know
what the "end" even is. This allows you to iterate through a
subsection of a sequence of values without having to create a new
object that represents this subsection.

You're trying to convert the iterator model into a different model of
iteration. Such a model is workable (what you want is a range-centric
model, where a range is the basic unit, and all operations on a range
produce other ranges. The position of a single element is a "range" of
one value), but it is not interchangeable with the iterator model.

Received on 2022-11-24 00:26:21