2

Taken from Understanding iterator/const_iterator implementation:

"although iterator and const_iterator are types declared in the scope of vector, there is no requirement that vector(or any STL container) have a member of either type - iterator and const_iterator are part of the interface of std::vector e.g. overloads of the member begin() returns those types, but nothing is said about how those function obtain the iterator they return"

Additionally STL containers must have:

"a begin and end function that returns iterators"

The above states that iterator and const_iterator are not required members of a STL container for example vector. I assume this means that the type returned from .begin or .end will differ based on implementation.

So I am wondering why this isn't problematic as I see a lot of people write out std::vector<someType>::iterator or std::vector<someType>::const_iterator where iterator and const_iterator are specified instead of using auto for example:

for (std::vector<int>::iterator i = s.begin(); i != s.end(); i++)
{

}
csguy
  • 1,354
  • 2
  • 17
  • 37

2 Answers2

7

You're reading the quote wrong. The person says

have a member of either type

not

have either type

When they say have a member of either type they mean there is no data member of type iterator or const_iterator in the class.

They do go on to say

iterator and const_iterator are part of the interface of std::vector

Which is correct as the standard requires that std::vector surface these types in it interface.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • but in https://en.cppreference.com/w/cpp/container/vector, iterator and const_iterator are member types listed – csguy Jan 15 '20 at 22:54
  • @csguy In this case they mean member as an actual data member, not member like the standard considers a type alias in a class to be. – NathanOliver Jan 15 '20 at 22:57
  • so iterator and const_iterator aren't "actual" members, they are type aliases in a container class and it is a requirement for a begin and end function to returns these aliases? – csguy Jan 15 '20 at 23:05
  • 1
    @csguy Correct. They are "member types" (aliases), they are not "members of type" (data members). They could be implemented in the container, or they could be implemented outside of the class and just referred to by the alias. It could also be that the alias refers to a plain pointer and that is the iterator used by the container. – super Jan 15 '20 at 23:09
2

std::vector will normally contain a typedef (or equivalently, using) to specify the type that the name iterator will refer to. At its simplest, it might be something like:

template <class T, class Allocator>
class vector {
    using iterator = T *;
    using const_iterator = T const *;
    // ...
};

Likewise, its begin and end must return iterators, and its cbegin and cend must return const_iterators. But, the vector object doesn't (necessarily need to contain any objects of type iterator or const_iterator.

Also note that although the names vector::iterator and vector::const_iterator need to be defined, it isn't strictly necessary that they be defined by vector itself. You could (for example) have vector derive from a base class defines the appropriate names:

template <class T>
class vector_base {
public:
    using iterator = T*;
    using const_iterator = T const *;
    // ...
};

template <class T, class Allocator>
class vector : public vector_base<T> {
};

So, even though vector itself doesn't define iterator, the name vector::iterator is well defined. I probably wouldn't bother to mention this except for one point: although it doesn't apply to vector, there is a standard class named std::iterator, that was intended primarily as a base class for iterators, and most of what it did was pretty much what's outlined above--define the member typedefs for value_type, difference_type, reference, and so on. Its use is now deprecated, but there are still a fair number of older iterator classes that used it (and older versions of the standard at least implied that most standard iterator types should use it as well).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111