This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-10-26


2271. Aliasing this

Section: 11.4.5  [class.ctor]     Status: C++17     Submitter: Richard Smith     Date: 2016-06-20

[Moved to DR at the November, 2016 meeting.]

The restrictions against aliasing this inside a constructor should apply to all objects, not just to const objects.

Proposed resolution (June, 2016):

Change 11.4.5 [class.ctor] paragraph 12 as follows:

During the construction of a const an object, if the value of the object or any of its subobjects is accessed through a glvalue that is not obtained, directly or indirectly, from the constructor's this pointer, the value of the object or subobject thus obtained is unspecified. [Example:

  struct C;
  void no_opt(C*);

  struct C {
    int c;
    C() : c(0) { no_opt(this); }
  };

  const C cobj;

  void no_opt(C* cptr) {
    int i = cobj.c * 100; // value of cobj.c is unspecified
    cptr->c = 1;
    cout << cobj.c * 100  // value of cobj.c is unspecified
         << '\n';
  }

  extern struct D d;
  struct D {
    D(int a) : a(a), b(d.a) {}
    int a, b;
  };
  D d = D(1);             // value of d.b is unspecified

end example]