Topic: value_type for output_iterator
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/03/19 Raw View
Konstantin Baumann wrote:
>
> Why is the "value_type" of an "output_iterator" (and
"back_insert_iterator<>", ...)
> "void"?
>
> IMHO it makes also sense to define the "value_type" of an
> "output_iterator".
>
> Suppose you have the following sequence of sequences:
>
> (
> (1),
> (1, 3),
> (6, 23, 42, 45),
> (123),
> (1, 2, 3, 4, 5, 6, 7),
> ...
> )
>
> This sequence could be of type:
> list<list<int> >
> or
> deque<vector<int> >
> or ...
>
> A generic function getting iterators to this sequence as
> input-parameters could look like:
>
> template<class InputIterator1>
> void in_func(InputIterator1 first, InputIterator1 last) {
>
> // getting the type of the inner sequence-container
> typedef typename iterator_traits<InputIterator>::value_type
> SequenceType2;
>
> // getting the type of the const_iterator of the inner
> // sequence-container
> typedef typename SequenceType2::const_iterator
> InputIterator2;
>
> for( ; first != last; ++first) {
>
> for(InputIterator2 inner = (*first).begin();
> inner != (*first).end();
> ++inner) {
>
> doSomething(*inner);
> }
> }
> }
>
> But you can't write a function returning such a sequence if you only
> know the type of the output-iterator of the outer-sequence:
>
> template<class OutputIterator>
> OutputIterator out_func(OutputIterator out) {
>
> // getting the type of the inner sequence-container
> typedef typename iterator_traits<OutputIterator>::value_type
> SequenceType2; // ERROR: getting "void"!!!
>
> while(...) {
>
> list<int> seq;
>
> while(...) {
>
> seq.push_back(...);
> }
>
> *(out++) = SequenceType2(seq.begin(), seq.end()); // FAILING!!!
> }
>
> return(out);
> }
In e-mail that I exchanged with Konstantin I learned that he was
instantiating this template for output_iterator, and that
> I'm using SGI's STL-implementation shipping with g++-2.8.1.
>
> In the header-file "stl_iterator.h" is the following:
>
> [...]
>
> struct output_iterator {
> typedef output_iterator_tag iterator_category;
> typedef void value_type; // <<<<<<<<<
> typedef void difference_type;
> typedef void pointer;
> typedef void reference;
> };
I believe that this is an invalid definition, at least according to CD2:
Section 24.3.1:
1 ... it is required that if Iterator is the type of an iterator, the
types
...
iterator_traits<Iterator>::value_type
...
be defineded as the iterator's ...., value type, ...
2 ...
template<class Iterator> struct iterator_traits{
...
typedef Iterator::value_type value_type;
...
I couldn't find a more direct statement, and there's nothing about it in
the iterator requirements, but the combination of these two statements
seem to me to require that Iterator::value_type must exist for all
iterators, and must be a typedef for the value type of the iterator. An
iterator with a value type of 'void' seems pointless. Am I missing
something? In particular, I'm suprised that output_iterator isn't
templated by its value_type.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: "Konstantin Baumann" <kostab@uni-muenster.de>
Date: 1998/03/16 Raw View
Why is the "value_type" of an "output_iterator" (and "back_insert_iterator<>", ...)
"void"?
IMHO it makes also sense to define the "value_type" of an
"output_iterator".
Suppose you have the following sequence of sequences:
(
(1),
(1, 3),
(6, 23, 42, 45),
(123),
(1, 2, 3, 4, 5, 6, 7),
...
)
This sequence could be of type:
list<list<int> >
or
deque<vector<int> >
or ...
A generic function getting iterators to this sequence as
input-parameters could look like:
template<class InputIterator1>
void in_func(InputIterator1 first, InputIterator1 last) {
// getting the type of the inner sequence-container
typedef typename iterator_traits<InputIterator>::value_type
SequenceType2;
// getting the type of the const_iterator of the inner
// sequence-container
typedef typename SequenceType2::const_iterator
InputIterator2;
for( ; first != last; ++first) {
for(InputIterator2 inner = (*first).begin();
inner != (*first).end();
++inner) {
doSomething(*inner);
}
}
}
But you can't write a function returning such a sequence if you only
know the type of the output-iterator of the outer-sequence:
template<class OutputIterator>
OutputIterator out_func(OutputIterator out) {
// getting the type of the inner sequence-container
typedef typename iterator_traits<OutputIterator>::value_type
SequenceType2; // ERROR: getting "void"!!!
while(...) {
list<int> seq;
while(...) {
seq.push_back(...);
}
*(out++) = SequenceType2(seq.begin(), seq.end()); // FAILING!!!
}
return(out);
}
--
Konstantin Baumann Westfaelische Wilhelms-Universitaet
Institut fuer Informatik (Zi. 603), Einsteinstr. 62, D-48149 Muenster
mailto:kostab@math.uni-muenster.de Tel:+49-251-83-32701
http://wwwmath.uni-muenster.de/cs/u/kostab/ Fax:+49-251-83-33755
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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 ]