Is “rebinding” references in C++ like this legal?

前端 未结 3 1814
天涯浪人
天涯浪人 2020-12-13 09:13

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 r

3条回答
  •  情深已故
    2020-12-13 09:44

    There is no reference being rebound in your example. The first reference (constructed on line two with the name r.r) is bound to the int denoted by x for the entire of its lifetime. This reference's lifetime is ended when the storage for its containing object is re-used by the placement new expression on line three. The replacement object contains a reference which is bound y for its entire lifetime which lasts until the end of its scope - the end of main.

提交回复
热议问题