Following the discussion on my answer to this question, apparently:
the following code is allowed
struct Foo {
int x;
};
Foo f;
Foo & f_ref = f;
(&f) -> ~Foo ();
new (&f) Foo ();
int x = f_ref .x;
but the following code is not allowed
struct Foo {
const int & x; // difference is const reference
Foo (int & i) : x(i) {}
};
int i;
Foo f (i);
Foo & f_ref = f;
(&f) -> ~Foo ();
new (&f) Foo (i);
int x = f_ref .x;
Because of $3.8/7
If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if:
- the type of the original object is not const-quali?ed, and, if a class type, does not contain any non-static data member whose type is const-quali?ed or a reference type ...
I can understand how a reference to f.x
could be invalidated when f ceases to exist, but I don't see why f_ref
should be invalidated purely because one of its members is const and/or reference and not otherwise: it was a reference to a Foo
before and is a reference to a Foo
afterwards.
Can someone please explain the rationale behind this condition?
Edit
Thanks for the answers. I don't buy the "guarantee it doesn't change" argument because we don't currently allow optimisers to cache referands, for example:
struct Foo {
const int & x;
Foo (const int & i) : x(i) {}
void do_it ();
};
int i;
Foo f (i);
const int & ii = f.x;
f .do_it (); // may modify i
std :: cout << ii; // May NOT use cached i
I don't see how do_it
is allowed to invalidate referenced values but operator new
isn't -- Sequence points invalidate cached values: why should delete/placement-new be exempt?