Should we still use classes or functions that require std::iterator if std::iterator is deprecated?

Viewed 444

I heard std::iterator is deprecated in C++17.

For example, like functions from <algorithm>, more likely we're going to be using begin() and end() member functions of objects which return iterator, like std::string, std::vector, etc.

Or like range-based for loops, where we will need begin() and end() which return iterator as well.

So therefore, if std::iterator as the base class is deprecated, should we even use member functions like begin() and end() or use other functions in the STL that require an iterator?

3 Answers

For example, like functions from <algorithm>

None of the functions from <algorithm> require std::iterator. It's fine to use them despite the deprecation of std::iterator.

more likely we're going to be using begin() and end() member functions

None of those require the use of std::iterator.

which return iterator as well.

Iterators are not deprecated. The class template std::iterator is deprecated.

should we even use member functions like begin() and end() or use other functions in the STL that require an iterator?

Yes. Although, I would recommend using them through std::ranges when convenient (in C++20).

So iterators inside of object/class like std::vector<int>::iterator are not inherited from std::iterator?

Standard library iterator types are defined by the standard library implementation. Technically, they could inherit from std::iterator but they are not required nor guaranteed to be inherited from it. Whether they inherit from std::iterator or not, they are not themselves deprecated.


std::iterator used to be intended to be used as a base class for custom iterators. Unless you are defining a custom iterator, you would not have been using it in the first place. If you are defining a custom iterator, you should not use std::iterator anymore. Note that it was never necessary to use std::iterator for this purpose.

The deprecation of std::iterator is purely deprecating it use as a base class.

Using begin() and end() and iterators in general isn't affected by this at all.

Basically, all this is saying is that when you define an iterator, you should have something like:

template <class T /* ... */>
class my_iterator {
    // the following names are what `std::iterator` defines for you:
    using iterator_category = std::forward_iterator_tag;
    using value_type = T;
    using reference = T&;
    using pointer = T*;
    using difference_type = std::ptrdiff_t;
    // ...
};

...and not:

template <class T /* .. */>
class my_iterator : public std::iterator<std::forward_iterator_tag, T> {
// ...
};

It also means the iterators in the standard library are intended to be done pretty much the same way (which they mostly have been, but depending on how you read it, there's an argument that older versions of the standard at least implied that they should be implemented by inheriting from std::iterator as well).

Since you mention begin() and end(), I suppose it's worth adding an unrelated point: it is generally better to use std::begin() and std::end() rather than the member functions of various collection classes. By doing this, your code can then work with built-in arrays, rather than just classes that define begin() and end() as member functions. But this is entirely unrelated to std::iterator being deprecated.

std::iterator is an iterator building block. It's not a valid iterator, and definitely not the iterator. The concept of iterators is by no means abandoned. There are no "classes or functions" in the C++ library that require std::iterator. None. There are classes and functions that use iterators in general, as a concept, but that has nothing whatsoever to do with std::iterator.

You have probably never used std::iterator anyway, since its only use from a user perspective is to implement custom iterators... So you'd have only used it knowingly if you implemented custom iterators and chose to derive them from std::iterator. The standard library was free to use std::iterator as it saw fit, but this was in implementation detail and there was no expectation in the C++ standard that any of the various iterators offered by the C++ library would actually use std::iterator.

Related