Why is std :: iterator deprecated?

The template class is std::iterator

set to be deprecated in C ++ 17. Why is this? This was a handy way to make sure it std::iterator_traits

works, especially if you can use the default template arguments. Is there any other way to do this in C ++ 17?

+26


source to share


1 answer


From the proposal that suggested rejection :

As an aid to writing the iterator classes, the original standard library provided an iterator class template to automate the declaration of the five typedefs expected from each iterator_traits iterator. This was then used in the library itself, for example in the spec std::ostream_iterator

:

template <class T, class charT = char, class traits = char_traits<charT> >
class ostream_iterator:
  public iterator<output_iterator_tag, void, void, void, void>;

      

The long sequence of arguments is void

much less clear to the reader than just providing the expected typedefs in the class definition itself, which is the approach taken by the current working draft, following the pattern set in C ++ 14 where we have deprecated inference across the entire functor library from unary_function

and binary_function

.

In addition to the diminished clarity, the iterator pattern also creates a trap for the unwary, as in typical use it will be a dependent base class, which means it will not look up while looking up the name from the class or its member functions. This leads to the surprise of users trying to understand why the following simple usage doesn't work:

#include <iterator>

template <typename T>
struct MyIterator : std::iterator<std::random_access_iterator_tag, T> {
   value_type data;  // Error: value_type is not found by name lookup 

   // ... implementations details elided ...
};

      

One reason for clarity was enough to convince the LWG to update the standard library specification to no longer require standard iterator adapters as deriving from std::iterator

, so no further use of this pattern in the standard itself is required. Therefore, he looks like a strong candidate for fatigue.

You can also see STL arguments in LWG 2438 . (h / t TC )




As for another way to do it, actually. Basically you can implement your own version std::iterator

(which is not too complicated) or manually write out all these typedefs (which is also not too difficult, and I actually prefer that for clarity).

+20


source







All Articles