Re: <map> erase
- From: "Stephen Howe" <sjhoweATdialDOTpipexDOTcom>
- Date: Wed, 5 Oct 2005 03:51:36 +0100
> Hmm, wait, I don't understand how that is proper. In ++x, the iterator is
> incremented before being passed to erase. In x++, the iterator is
> incremented after being passed to erase...
Nope. The old value of x is what is passed to map erase.
But x is incremented to the next iterator position _before_ the call to
erase().
It is about what side effects are completed when a "sequence point" occurs I
believe -- from C.
After erase() completes, the old value of x is no longer a valid iterator.
But that does not matter as x is now at the next position -- and still
valid.
Post-increment is exactly what is wanted.
>..., but at that point it is invalidated. Even if you were to make a copy
>of x then increment it after erase it would not be valid.
>
> So then I am confused, does that mean that x++ copies x to t, then
> increments x, then returns t? That's the only way I can see how the below
> would work.
That is exactly what it does. If you have the snippet
extern int func(int, int, int);
int x = 1;
int y = 2;
int z = 3;
func(x++, y++, z++);
then for C & C++, the compilers are supposed to pass the current values of
x, y, z to func() but before func() is called, the increments are supposed
to have happened. So a compiler is free to implement it is
// A
int xold = x;
int yold = y;
int zold = z;
x += 1;
y += 1;
z += 1;
// B
func(xold, yold, zold);
and the statements between A & B can occur in any order as long as recording
of old value occurs before appropriate increment
But this is an invalid translation:
// A
int xold = x;
int yold = y;
int zold = z;
// B
func(xold, yold, zold);
x += 1;
y += 1;
z += 1;
The increments are not supposed to occur after the function call.
Stephen Howe
.
- Follow-Ups:
- Re: <map> erase
- From: aa
- Re: <map> erase
- References:
- <map> erase
- From: Ian Semmel
- Re: <map> erase
- From: Stephen Howe
- Re: <map> erase
- From: Jason Winnebeck
- <map> erase
- Prev by Date: Re: <map> erase
- Next by Date: Re: <map> erase
- Previous by thread: Re: <map> erase
- Next by thread: Re: <map> erase
- Index(es):
Relevant Pages
|