Re: strange crash after assertion with std::map::iterator
"Mycroft Holmes" <m.holmes@nospam.it> wrote in message
news:eYqIVbbgHHA.2396@TK2MSFTNGP04.phx.gbl
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:%238XGXIbgHHA.4156@TK2MSFTNGP02.phx.gbl...
"Mycroft Holmes" <m.holmes@nospam.it> wrote in message
news:etWO$PZgHHA.4416@TK2MSFTNGP03.phx.gbl
maybe I wasn't clear enough with the example.
The ORIGINAL code has a template type iterator_t and it tests -let's
say-
bool test_if_invalid(iterator_t i)
{
return i == iterator_t();
}
Well, now you know not to do that. There is no general way to test
whether an iterator is invalid,
Well, the test is valid for some iterators (pointers, input
iterators, I think).
That's why I say "general". I'm not sure what you mean when you say your
test is valid for pointers, though. You seem to want to use it this way,
which is unlikely to work for pointers:
int* p;
test_if_invalid(p); // will likely declare the pointer "valid"
Also, by input iterators you probably mean stream iterators. I'm not
sure it is meaningful to say istream_iterator() is invalid. It plays the
same role for stream iterators as container.end() plays for iterators
over a container. Would you consider container.end() to be invalid
iterator?
Finally, consider:
vector<int> v;
v.push_back(1);
vector<int>::iterator i = v.begin();
v.push_back(2);
At the end of this sequence, 'i' is invalid. Would you expect to be able
to test for it? If so, how would you go about it, if you were writing
the C++ standard? How would you go about it if you were STL implementor?
So what I'm saying is that it's rather unfortunate that the standard
does not enforce such a requirement for all iterators, since the
implementation would be trivial.
You can submit a proposal to standardization committee, if you feel
strongly about it.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925