Since I know an iterator in the program could be invalidated by some previous operation, I want to invalidate it explicitly. Such as assign NULL to a pointer to invalidate it, I just want to do the same on iterator. container.end() could not be the precise idea here. I tried to assign NULL to my iterator, but it failed. How can I get the same behavior of NULL pointer on iterator?
-
First, I think it is not good idea to such behaviour. Second, you can store pointer to iterator.Sergey Miryanov– Sergey Miryanov2010-12-06 05:41:45 +00:00Commented Dec 6, 2010 at 5:41
-
1if you really want someone may also explain how to get Undefined Behavior explicitely ;-)kriss– kriss2010-12-06 07:08:37 +00:00Commented Dec 6, 2010 at 7:08
-
Why would you want this? There's nothing you can do with the iterator afterwards that you couldn't do before.MSalters– MSalters2010-12-06 10:01:22 +00:00Commented Dec 6, 2010 at 10:01
Add a comment
|
4 Answers
could be invalidated by some previous operation, I want to invalidate it explicitly
... If you know it may not be valid, then all you have to do it stop using it. Invalidating the iterator explicitly accomplishes nothing, because using it is a programming error either way.
1 Comment
Tobias Langner
try to limit the scope of your iterator by using additional blocks. Outside the block, the iterator isn't there anymore. That's better than invalid.
Shouldn't this always work? (iterators are always assignable and default-constructible)
template <typename Iter>
void foo(Iter it){
it = Iter(); // invalidate
}
2 Comments
Thomson
This is very cool. I can get the iterator type directly, so the function is unnecessary. By the way, I think we need to pass
it by reference, is this right?Stack Overflow is garbage
in order to modify it, sure. This was just an example. The only reason I made it a function was that it was the easiest way to say "Assuming the iterator type is
Iter and the iterator object is called it..." :)