9

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Jack Murrow
  • 396
  • 3
  • 11
  • 3
    https://www.fluentcpp.com/2018/05/08/std-iterator-deprecated/ – Andrew Truckle Dec 28 '20 at 22:44
  • 1
    I guess the real question would be: Have you ever **used** `std::iterator`? If you grep all the C++ code you ever wrote, how many instances of `std::iterator` (the literal, exact text) can you find? I bet that you never used it, and it's deprecated precisely because it's mostly unnecessary, and partly confusing. – Kuba hasn't forgotten Monica Dec 29 '20 at 04:25

3 Answers3

13

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • So iterators inside of object/class like `std::vector::iterator` are not inherited from `std::iterator`? – Jack Murrow Dec 28 '20 at 22:48
  • 3
    They are not required to inherit from `std::iterator`. Until it is completely removed from the C++ standard they may inherit from it, or they may not. Whether they do or not is immaterial. – Sam Varshavchik Dec 28 '20 at 22:53
7

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.

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

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313