Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 998
Jack Murrow Avatar asked Dec 28 '20 22:12

Jack Murrow


People also ask

Is STD iterator deprecated?

std::iterator is deprecated, so we should stop using it. Indeed, the next step after deprecation could be total removal from the language, just like what happened to std::auto_ptr .

What is std :: iterator?

std::iterator is the base class provided to simplify definitions of the required types for iterators.


Video Answer


2 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.

like image 97
eerorika Avatar answered Oct 21 '22 11:10

eerorika


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.

like image 29
Jerry Coffin Avatar answered Oct 21 '22 10:10

Jerry Coffin