Movatterモバイル変換


[0]ホーム

URL:


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

2025-12-20


760.this inside a nested class of a non-static member function

Section:_N4567_.5.1.1  [expr.prim.general]    Status:CD2    Submitter:Mike Miller    Date:3 February, 2009

[Voted into WP at March, 2010 meeting.]

this is a keyword and thus not subject to ordinary namelookup. That makes the interpretation of examples like the followingsomewhat unclear:

    struct outer {      void f() {        struct inner {          int a[sizeof(*this)];  // #1        };      }    };

According to _N4567_.5.1.1 [expr.prim.general] paragraph 3,

The keywordthis shall be used only inside a non-static classmember function body (11.4.2 [class.mfct]) or in abrace-or-equal-initializer for a non-static data member.

Should the use ofthis at #1 be interepreted as a well-formedreference toouter::f()'sthis or as an ill-formedattempt to refer to athis forouter::inner?

One possible interpretation is that the intent is as ifthiswere an ordinary identifier appearing as a parameter in each non-staticmember function. (This view applies to the initializers of non-staticdata members as well if they are considered to be rewritten asmem-initializers in the constructor body.) Under thisinterpretation, the prohibition against usingthis in othercontexts simply falls out of the fact that name lookup would fail tofindthis anywhere else, so the reference in the example iswell-formed. (Implementations vary in their treatment of thisexample, so clearer wording is needed, whichever way theinterpretation goes.)

Proposed resolution (February, 2010):

Change _N4567_.5.1.1 [expr.prim.general] paragraph 2 as follows:

...The keywordthis shall be used only insidethebody of a non-staticclass member functionbody (11.4.2 [class.mfct])of the nearestenclosing class or in abrace-or-equal-initializerfor a non-static data member(11.4 [class.mem]). The type of the expression is a pointer tothe class of the function or non-static data member, possiblywith cv-qualifiers on the class type. The expression is anrvalue.[Example:

  class Outer {    int a[sizeof(*this)];            // error: not inside a member function    unsigned int sz = sizeof(*this); // OK, in brace-or-equal-initializer    void f() {      int b[sizeof(*this)];          // OK      struct Inner {        int c[sizeof(*this)];        // error: not inside a member function ofInner      };    }  };

end example]




[8]ページ先頭

©2009-2026 Movatter.jp