Is "rebinding" references in C++ like this legal?
c++, language-lawyer, placement-new, reference
Solution
I think I found the answer in a passage below the "quoted" one that talks about trivial dtor / dtor side effects, namely [basic.life]/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 storage for the new object exactly overlays the storage location which the original object occupied, and
the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and
the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type, and
the original object was a most derived object of type `T` and the new object is a most derived object of type `T` (that is, they are not base class subobjects).
By reusing the storage, we end the lifetime of original object [basic.life]/1
The lifetime of an object of type `T` ends when:
if `T` is a class type with a non-trivial destructor, the destructor call starts, or
the storage which the object occupies is reused or released.
So I think [basic.life]/7 covers the situation
Reference<int> r(x);
new (&r) Reference<int>(y);
where we end the lifetime of the object denoted by `r`, and create a new object at the same location.
As `Reference<int>` is a class type with a reference data member, the requirements of [basic.life]/7 are not fulfilled. That is, `r` might not even refer to the new object, and we may not use it to "manipulate" this newly created object (I interpret this "manipulate" also as read-only accesses).
Problem
Is the following legal in C++? As far as I can tell, `Reference` has a trivial destructor, so it should be legal. But I thought references can't be rebound legally... can they? ``` template<class T> struct Reference { T &r; Reference(T &r) : r(r) { } }; int main() { int x = 5, y = 6; Reference<int> r(x); new (&r) Reference<int>(y); } ```
Related problems
- What are aggregates and trivial types/PODs, and how/why are they special?
- Do we need to explicitly call the destructor for the "simple POD classes" allocated with "placement new"?
- Is it UB to re-use an object's storage without destroying it first?
- Is it dangerous to use placement new on an old object without explicitly calling the destructor first?
- Observable behavior and undefined behavior -- What happens if I don't call a destructor?