Re: What is the best way to iterate over a map? (or any paired container)
Earl Purple wrote:
kanze wrote:
Been there, done that. I'm actually pretty good at writing
non-conformant iterators... that work most of the time (and
pass all of my own tests).
What I've found useful in such cases is to compiler with a
concept checking version of the library. A recent version of
g++, with -D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC can find a lot of errors.
How does it know though if the class is intended to be an iterator? Is
it because you try to pass it to any standard algorithm? Or does merely
calling it iterator or returning it from a begin() function suddenly
turn it into one?
It checks arguments to the standard algorithms, to ensure that
they meet the requirements for those arguments. Some standard
algorithms have a requirement that the argument meet the
requirements of e.g. forward iterator. G++ verifies that any
type you pass as a parameter to those algorithms meets *all* of
the stated requirements (and thus, all of the requirements of a
forward iterator, if that's what the algorithm says it
requires), even if some of the characteristics aren't used in
the actual implementation.
Although the problems it detected in my code all had to do with
iterators, I presume that it also verifies other types of
arguments, e.g. Predicate, etc. I also suppose that it isn't
perfect; I'd be very surprised, for example, if it detected that
your ordering predicate didn't specify a complete ordering.
Still, it's definitly worth using.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]