Topic: iterator::value_type usage
Author: Paul Smyth <paul.smyth@metrics.co.uk>
Date: 1999/02/18 Raw View
Hello,
I'm trying to write an algorithm which takes a range of iterators, and
creates some temporary container objects while processing.
The problem is that the implementations I've tried of STL (for MSVC++ 5.0
and gcc 2.7.2 (Cygnus)) won't let me find the value_type from a
vector<T>::iterator, although they work fine with other iterators.
This seems to be because the other iterators are actually classes defined
within the container definition which are properly derived from iterator,
which defines value_type.
However, for vector<T>, vector<T>::iterator is just a typedef for T* , which
means you (of course) can't find a value_type from it.
So the question is, is this something that I should expect to be able to do?
Does the standard say I should be able to do it? Is it just a case of the
implementations available not supporting this part of the standard?
Thank you,
Paul Smyth.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Jonathan H Lundquist" <fluxsmith@fluxsmith.com>
Date: 1999/02/18 Raw View
It is something you *should* be able to do via iterator_traits. However,
MSCV doesn't support it because MSVC doesn't support partial specialization.
That is why in the SGI implementation you will find a function, value_type,
which returns a typed pointer. This should work even with vector::iterator.
Paul Smyth <paul.smyth@metrics.co.uk> wrote in message
news:998331E4E8A4D011A02500A024F00AE707F09E@host2.oxfordmetrics.co.uk...
>The problem is that the implementations I've tried of STL (for MSVC++ 5.0
>and gcc 2.7.2 (Cygnus)) won't let me find the value_type from a
>vector<T>::iterator, although they work fine with other iterators.
>This seems to be because the other iterators are actually classes defined
>within the container definition which are properly derived from iterator,
>which defines value_type.
>
>However, for vector<T>, vector<T>::iterator is just a typedef for T* ,
>which means you (of course) can't find a value_type from it.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/02/18 Raw View
Paul Smyth wrote:
>
> Hello,
>
> I'm trying to write an algorithm which takes a range of iterators, and
> creates some temporary container objects while processing.
>
> The problem is that the implementations I've tried of STL (for MSVC++ 5.0
> and gcc 2.7.2 (Cygnus)) won't let me find the value_type from a
> vector<T>::iterator, although they work fine with other iterators.
> This seems to be because the other iterators are actually classes defined
> within the container definition which are properly derived from iterator,
> which defines value_type.
>
> However, for vector<T>, vector<T>::iterator is just a typedef for T* , which
> means you (of course) can't find a value_type from it.
>
> So the question is, is this something that I should expect to be able to do?
> Does the standard say I should be able to do it? Is it just a case of the
> implementations available not supporting this part of the standard?
Use iterator_traits<vector<T>::iterator>::value_type. For generic T,
iterator_traits<T> expects T to be a class with a value_type typedef.
However, iterator_traits<T*> is specialized to set value_type to T.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Matt Austern <austern@sgi.com>
Date: 1999/02/18 Raw View
Paul Smyth <paul.smyth@metrics.co.uk> writes:
> The problem is that the implementations I've tried of STL (for MSVC++ 5.0
> and gcc 2.7.2 (Cygnus)) won't let me find the value_type from a
> vector<T>::iterator, although they work fine with other iterators.
> This seems to be because the other iterators are actually classes defined
> within the container definition which are properly derived from iterator,
> which defines value_type.
>
> However, for vector<T>, vector<T>::iterator is just a typedef for T* , which
> means you (of course) can't find a value_type from it.
>
> So the question is, is this something that I should expect to be able to do?
> Does the standard say I should be able to do it? Is it just a case of the
> implementations available not supporting this part of the standard?
No, the standard does not guarantee that, for every iterator type
Iter, you can find its value type by writing Iter::value_type. As
you've observed, you can't do it for pointers, and pointers are
iterators.
What the standard does guarantee is that you can find an iterator's
value type by writing iterator_traits<Iter>::value_type. This does
work for pointers, and it must work for every other kind of iterator
as well.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]