placement new on a class with reference field

前端 未结 2 449
礼貌的吻别
礼貌的吻别 2020-12-17 19:08

This is a code example from the C++20 spec ([basic.life]/8):

struct C {
  int i;
  void f();
  const C& operator=( const C& );
};

const C& C::o         


        
2条回答
  •  伪装坚强ぢ
    2020-12-17 19:38

    It is legal to replace objects with const-qualified and reference non-static data members. And now, in C++20, [the name of|a [pointer|reference] to] the original object will refer to the new object after replacement. The rules has been changed in response to RU007/US042 NB comments http://wg21.link/p1971r0#RU007:

    RU007. [basic.life].8.3 Relax pointer value/aliasing rules

    ...

    Change 6.7.3 [basic.life] bullet 8.3 as follows:

    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-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type neither a complete object that is const-qualified nor a subobject of such an object, and

    • ...

提交回复
热议问题