Re: std::map, does the standard define what to do here?
On Apr 7, 11:03 pm, Zachary Turner <divisorthe...@gmail.com> wrote:
On Apr 7, 4:51 am, rishabh <rishabh.l...@gmail.com> wrote:
On Apr 7, 3:01 am, peter koch <peter.koch.lar...@gmail.com> wrote:
On 6 Apr., 23:39, Zachary Turner <divisorthe...@gmail.com> wrote:
std::map<int, int> test;
bool eq1 = false;
bool eq2 = false;
test[5] = 7;
std::map<int, int>::iterator iter = test.begin();
std::map<int, int>::iterator iterEnd = test.end();
eq1 = (iter == iterEnd);
--iter;
eq2 = (iter == iterEnd);
I suspect the answer is that no, decrementing the begin
iterator is undefined, but just looking for
confirmation. In Visual C++, after this code executes
eq1 is false and eq2 is true(!), and in fact in Visual
C++ you can decrement this iterator indefinitely and it
will just cycle through the tree forever.
You are correct - it is undefined behaviour, so don't do it.
eq2 is never true even you decrease it more than 1 time.
Feel free to try it on Visual Studio 2005, I assure you it
most definitely is true. I have only tried it with a map
containing exactly 1 item, in which case decrementing .begin()
DOES EQUAL .end(), so obviously decrementing it again will
yield a valid element, and decrementing it again will equal
end again, ad infinitum.
It's obviously counterintuitive, which is why I assumed that
the standard either a) explicitly requires that such behavior
not exist, in which case that would mean Visual Studio's STL
implementation has a bug, or b) does not specify the behavior,
or leaves it undefined, in which case Visual Studio's STL
implementation is fine.
For a weak-enough definition of fine. Generally speaking, VC++
traps this sort of error, at least when compiled with the right
options. (Try it with and std::vector, for example. If the
code doesn't abort, you're not using the right compiler
options.) So I rather suspect that this could be considered a
bug. (I get a runtime error with all of the sequence
containers, but none for the associative containers, with VC++.
I get a runtime error with all of the containers with g++.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34