C++ Logo

std-discussion

Advanced search

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

From: Will Hawkins <hawkinsw_at_[hidden]>
Date: Fri, 29 Jan 2021 19:15:20 -0500
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

?

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 18:15:35