Iterators wrappers?
Paolo Carlini
pcarlini@suse.de
Wed Aug 11 17:10:00 GMT 2004
Hi,
> I am currently working on some test cases for some functions in the
> algorithm header. I wondered if there exists any "exact iterator
> checkers", which (for example) given the begin() and end() iterators
> for a vector would only reveal exactly the required features of a
> bidirectional iterator and perhaps also further check for things like
> an iterator being moved after end() or before begin(). Such things
> would allow for more easily testing if an algorithm is using exactly
> the properties it is allowed to.
In 3.4.x, to help the programmer with this kind of issues, besides the
compile-time concept-checks, we have a new, powerful, run-time debug mode:
http://gcc.gnu.org/onlinedocs/libstdc++/debug.html
To give you a stupid, trivial example, this snippet compiled with
-D_GLIBCXX_DEBUG:
#include <vector>
int main()
{
std::vector<int> v;
std::vector<int>::iterator it = v.end();
*it;
}
leads, at run time, to:
/usr/local/gcc-341/lib/gcc/i686-pc-linux-gnu/3.4.1/../../../../include/c++/3.4.1/debug/safe_iterator.h:178:
error: attempt to dereference a past-the-end iterator.
Objects involved in the operation:
iterator "this" @ 0x0xbffff690 {
type =
N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIPiN10__gnu_norm6vectorIiSaIiEEEEEN15__gnu_debug_def6vectorIiS6_EEEE
(mutable iterator);
state = past-the-end;
references sequence with type `N15__gnu_debug_def6vectorIiSaIiEEE' @
0x0xbffff690
}
Abort
/////////////////
Paolo.
More information about the Libstdc++
mailing list