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

2024-12-19


1837. Use of this in friend and local class declarations

Section: _N4567_.5.1.1  [expr.prim.general]     Status: CD6     Submitter: Hubert Tong     Date: 2014-01-13

[Accepted at the November, 2020 meeting as part of paper P1787R6 and moved to DR at the February, 2021 meeting.]

The description of the use of this found in _N4567_.5.1.1 [expr.prim.general] paragraphs 3 and 4 allow it to appear in the declaration of a non-static member function following the optional cv-qualifier-seq and in the brace-or-equal-initializer of a non-static data member; all other uses are prohibited. These restrictions appear to allow questionable uses of this in several contexts. For example:

  template <typename T>
  struct Fish { static const bool value = true; };

  struct Other {
    int p();
    auto q() -> decltype(p()) *;
  };

  class Outer {
    // The following declares a member function of class Other.
    // Is this interpreted as Other* or Outer*?
    friend auto Other::q() -> decltype(this->p()) *;
    int g();
    int f() {
     extern void f(decltype(this->g()) *);
     struct Inner {
       // The following are all within the declaration of Outer::f().
       // Is this Outer* or Inner*?
       static_assert(Fish<decltype(this->g())>::value, "");
       enum { X = Fish<decltype(this->f())>::value };
       struct Inner2 : Fish<decltype(this->g())> { };
       friend void f(decltype(this->g()) *);
       friend auto Other::q() -> decltype(this->p()) *;
     };
     return 0;
    }
  };
  struct A {
   int f();
   bool b = [] {
    struct Local {
     static_assert(sizeof this->f() == sizeof(int), ""); // A or Local?
    };
   };
  };

There is implementation divergence on the treatment of these examples.