C++ Logo

std-discussion

Advanced search

Re: Question on forward iterator requirements (especially paragraph 2 of 23.3.5.5)

From: Will Hawkins <hawkinsw_at_[hidden]>
Date: Fri, 29 Jan 2021 20:15:17 -0500
On Fri, Jan 29, 2021 at 7:15 PM Will Hawkins <hawkinsw_at_[hidden]> wrote:

> Hello everyone!
>
> I hope that these are reasonable questions and not a waste of time.
>
> First, I have been reading the requirements for forward iterators in
> 23.3.5.5 and am focused on Paragraph 2 [1].
>
> Given,
> - The vector v in std::vector<int> v{}; is an empty sequence, and
> - The vector iterator v_it in std::vector<v>::iterator v_it{}; is a
> value-initialized iterator that meets the requirements of forward iterator,
>
> is it specified (per Note 1 of Paragraph 2) that
>
> v.end() == v_it
>
> ?
>

After further discussion with another language expert, I have been
convinced that the paragraph does not specify that v.end() must equal v_it.
Rather, it only specifies that

v_it == u_it

where

std::vector<v>::iterator v_it{};
std::vector<v>::iterator u_it{};

Is this the correct conclusion to draw from that paragraph?

My confusion arose from the use of the phrase "same empty sequence" in Note
1. I was reading that to mean "all empty sequences of the same type".

I would love to know why Note 1 was included. It seems to add ambiguity
rather than clarify. Thoughts?

Thanks again for taking the time to engage on this point!

Will


>
> To say it a different way, does that paragraph and its note specify what
> this should print?
>
> #include <iostream>
> #include <vector>
>
> int main() {
> std::vector<int> int_vector{};
> std::vector<int>::iterator int_vector_it{};
>
> if (int_vector_it == int_vector.end())
> std::cout << "int_vector_it == int_vector.end()\n";
> else
> std::cout << "int_vector_it != int_vector.end()\n";
> return 1;
> }
>
> Second, forward iterators are required to be default constructible. I can
> find no requirement in the standard for the semantics of a
> default-initialized forward iterator's value. Is it specified as
> implementation defined? The only clue comes from [2] which could be read to
> imply that a default-initialized iterator should follow the specification's
> semantics for default-initialized pointers.
>
> These issues have been driving me mad for several days. Again, I hope that
> they are reasonable questions and that I am not wasting your time.
>
> Thanks in advance for any responses!
> Will
>
>
> [1] https://eel.is/c++draft/forward.iterators#2
> [2] https://eel.is/c++draft/iterator.requirements#general-7
>
>

Received on 2021-01-29 19:15:33