Topic: operator bool() for iterators?
Author: dirk@sallust.ida.ing.tu-bs.de (Dirk Herrmann)
Date: 1996/07/24 Raw View
Hello!
When using iterators of the standard library, it seems to require
some typing overhead when proceeding through a list or something:
// code not verified, please excuse typing mistakes
for (list<X>::iterator i(x.begin); i != x.end(); i++) {
...
}
What bothers me is the need to type 'i != x.end()' instead of 'i':
Assume there would be a value for an iterator, which was declared
'special' like the value NULL is for pointers. A sensible conversion
from any iterator to bool then could be a test for this 'NULL'.
For the code above it is not that disgusting, but as soon as you use
longer names for 'X' and 'x' this verbosity leades to more lines of
code by having to split the for-statement line. (I'm not counting
characters here, but having more code without gaining clarity is
not desirable.)
Does someone know whether there is planned to add such conversions
to bool, or maybe if there already is some way to avoid the
verbosity shown above?
--
Best regards,
Dirk Herrmann
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Marco Dalla Gasperina" <marcodg@hp-vcd.vcd.hp.com>
Date: 1996/07/25 Raw View
> dirk@sallust.ida.ing.tu-bs.de (Dirk Herrmann) wrote in article
<4t53vv$71q@ra.ibr.cs.tu-bs.de>...
> Hello!
Hi
>
> When using iterators of the standard library, it seems to require
> some typing overhead when proceeding through a list or something:
>
> // code not verified, please excuse typing mistakes
> for (list<X>::iterator i(x.begin); i != x.end(); i++) {
> ...
> }
>
> What bothers me is the need to type 'i != x.end()' instead of 'i':
> Assume there would be a value for an iterator, which was declared
> 'special' like the value NULL is for pointers. A sensible conversion
> from any iterator to bool then could be a test for this 'NULL'.
One of the things that STL has is consistent use of intervals.
Everything works on [start,finish) and you always know you're done
when some iterator i == finish (stepping from start to finish).
Your auto conversion to bool would be really hard for two reasons:
(1) sometimes you don't step from c.begin() to c.end() in which case
the iterator needs to know when it's done and not the code using
the iterator. And (2) sometimes iterators are just pointers in which
case there already is a conversion to bool ( i.e. p != 0 ) which may.
not be what you want. So, best to make it all the same.
>
> For the code above it is not that disgusting, but as soon as you use
> longer names for 'X' and 'x' this verbosity leades to more lines of
> code by having to split the for-statement line. (I'm not counting
> characters here, but having more code without gaining clarity is
> not desirable.)
But more code so that any code which uses iterators is identical
adds a great deal of clarity.
marco
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jbuck@Synopsys.COM (Joe Buck)
Date: 1996/07/25 Raw View
dirk@sallust.ida.ing.tu-bs.de (Dirk Herrmann) writes:
>When using iterators of the standard library, it seems to require
>some typing overhead when proceeding through a list or something:
>
>// code not verified, please excuse typing mistakes
>for (list<X>::iterator i(x.begin); i != x.end(); i++) {
> ...
>}
>
>What bothers me is the need to type 'i != x.end()' instead of 'i':
It would have been possible to use a scheme in which null iterators mark
ends of ranges, but it would be less powerful to require it. In STL,
pairs of iterators mark a range which includes the item pointed to by the
first iterator and excludes the value pointed to by second iterator. Your
idea is OK if all you ever want to do is process all the values in the
container (that is, your endpoints are always x.begin() and x.end()).
But consider: the STL scheme allows you to treat any sequence of the
list as if it were a list (the end iterator need not point to the end
of the list). Your scheme would require copying the subsequence into
a new list.
If you want compact source code, you might try
for_each(x.begin(), x.end(), function);
or whatever STL algorithm might be appropriate -- this eliminates
the lengthy declaration of the iterator and tests on it.
>Assume there would be a value for an iterator, which was declared
>'special' like the value NULL is for pointers. A sensible conversion
>from any iterator to bool then could be a test for this 'NULL'.
You are free to define an iterator class so that it has such a conversion.
>Does someone know whether there is planned to add such conversions
>to bool, or maybe if there already is some way to avoid the
>verbosity shown above?
It would not be possible to do for vector's iterators, since these are
pointers and end() always points just past the end of the data. The
conversion to bool would always return true.
--
-- Joe Buck <jbuck@synopsys.com> (not speaking for Synopsys, Inc)
Work for something because it is good,
not just because it stands a chance to succeed. -- Vaclav Havel
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/07/25 Raw View
dirk@sallust.ida.ing.tu-bs.de (Dirk Herrmann) writes:
> When using iterators of the standard library, it seems to require
> some typing overhead when proceeding through a list or something:
>
> // code not verified, please excuse typing mistakes
> for (list<X>::iterator i(x.begin); i != x.end(); i++) {
> ...
> }
>
> What bothers me is the need to type 'i != x.end()' instead of 'i':
> Assume there would be a value for an iterator, which was declared
> 'special' like the value NULL is for pointers. A sensible conversion
> from any iterator to bool then could be a test for this 'NULL'.
>
> For the code above it is not that disgusting, but as soon as you use
> longer names for 'X' and 'x' this verbosity leades to more lines of
> code by having to split the for-statement line. (I'm not counting
> characters here, but having more code without gaining clarity is
> not desirable.)
>
> Does someone know whether there is planned to add such conversions
> to bool, or maybe if there already is some way to avoid the
> verbosity shown above?
One of the constraints in defining the STL iterator sequence was that a
pointer into a C-style array must be a valid iterator over C-style
arrays. Since simply incrementing a pointer beyond the end of an array
does not yield anything that can be meaningfully tested as an end
criterium, STL iterators do not do this either.
This was a fundamental design choice. You can agree, or disagree, but
any other choice results in a completely different set of idioms.
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Sean A Corfield <sean@ocsltd.com>
Date: 1996/07/29 Raw View
In article <4t53vv$71q@ra.ibr.cs.tu-bs.de>,
dirk@sallust.ida.ing.tu-bs.de (Dirk Herrmann) wrote:
|> What bothers me is the need to type 'i != x.end()' instead of 'i':
Of course there's no reason why the 'end' iterator should in fact be
'x.end()'. Iterators allow you to operate on a range of objects, the range
is delimited by a pair of iterators [first,last) so there is no way for a
'conversion to bool' to know when the iterator is at the end of the range.
To iterate over an entire container, you could always write your own cursor
class:
template<typename Container> class Cursor {
public:
Cursor( Container& cc ) : c( cc ), it( c.begin() ) { }
operator bool() const { return it != c.end(); }
typename Container::reference operator*() const { return *it; }
Cursor& operator++() { ++it; return *this; }
//... etc
private:
Container& c;
typename Container::iterator it;
};
for ( Cursor< List<X> > c = x; c; ++c )
... *c ...
// also unverified code, but note the use of 'typename'
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]