Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are super-SCARY iterators legal?

I understand that the standard allows std::vector<int, A> to have the same type of iterators for different allocators A. This is called SCARY iterators.

Now the question is does the standard allow std::vector<int, A>::iterator be simply a typedef of A::pointer, thus making it just an int* for the default allocator?

Or is there some (implicit) requirement for it to be a separate class type per container? If there is no such requirement then why all major implementations (including the SCARY ones) don't use this approach? It would presumably reduce compiler work even further, though now code that overloads on int* and vector<>::iterator will not compile.

like image 612
Yakov Galka Avatar asked Jul 19 '16 21:07

Yakov Galka


People also ask

Should I use iterators?

Convenience in programming: It is better to use iterators to iterate through the contents of containers as if we will not use an iterator and access elements using [ ] operator, then we need to be always worried about the size of the container, whereas with iterators we can simply use member function end() and iterate ...

Should I use iterators in c++?

Use of Iterators in C++ Iterator algorithms are not dependent on the container type. An iterator can be used to iterate over the container elements. It can also provide access to those elements to modify their values. Iterators follow a generic approach for STL container classes.


1 Answers

Re

does the standard allow std::vector<int, A>::iterator be simply a typedef of A::pointer

As far as I know, yes. But not std::vector<bool, A>, because that's a specialization where a dereferenced iterator is a proxy object that accesses whatever representation is used, with the intent of supporting one bit per bool.

like image 127
Cheers and hth. - Alf Avatar answered Oct 11 '22 03:10

Cheers and hth. - Alf