Re: Should value_type, reference_type, etc. be in standard iterator types?
on Tue Mar 27 2007, "Chris Jefferson" <4zumanga-AT-gmail.com> wrote:
Here is an interesting question that came up recently. Is the
following standards conforming?
list<int>::iterator::value_type t;
It works on every compiler I have access to and I've seen a lot of
people using it, but I'm now not sure the standard requires it work.
It does not. The correct way to access the iterator's value_type is
through iterator_traits.
The most obvious manner of making sure iterator_traits works for non-
pointer iterator types is to insert value_type, difference_type, etc.
into the iterator. However, is this required?
No
I can think of two ways
around this:
1) Specialise iterator_traits for the iterators of each of the
standard containers
2) Make iterator_traits a friend of the iterators and make the
typedefs private.
Right
The reason this came up originally is:
vector<int>::iterator::value_type t;
In compilers where vector::iterator is a raw pointer, this code
clearly won't work. In the case where iterator is not a pointer, I can
think of 3 readings of the standard.
1) This has to work, as the standard doesn't allow messing around with
the definition of iterator_traits.
That is not the intention of the standard. The standard allows
specialization of standard library types so long as they retain the
right semantics.
2) This may or may not work depending on how vector<int>::iterator is
implemented.
3) Implementations should try to stop this working as a QOI issue.
Any comments / suggestions? Would it possibly be worth saying this
typedefs have to be defined, simply because they are so frequently
defined? Perhaps even require that vector::iterator is a class in C+
+09 just to standardise behaviour?
There are many things about iterators that should be fixed, but I
don't think this is on the list.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]