cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[expr.prim.lambda.capture] p11 id-expression is not a postfix-expression #440

Closed xmh0511 closed 9 months ago

xmh0511 commented 9 months ago

Full name of submitter (unless configured in github; will be published with the issue): Jim X

[expr.prim.lambda.capture] p11 says

Every id-expression within the compound-statement of a lambda-expression that is an odr-use ([basic.def.odr]) of an entity captured by copy is transformed into an access to the corresponding unnamed data member of the closure type.

Consider this case

#include <iostream>
struct A{
    int a;
    void foo(){
        auto f = [*this]() mutable{
            a = 1;
        };
    }
};

First, [class.mfct.non.static] p2 says

the id-expression is transformed into a class member access expression ([expr.ref]) using (*this) as the postfix-expression to the left of the . operator.

So, the expression at #1 is transformed to (*this).a = 1;. In this example, (*this) is a postfix-expression, which is also primary-expression with the form (expression), which is also ( unary-operator cast-expression ) , where the unary-operator is * and cast-expression is the primary-expression this, whatever the whole postfix-expression in the class member access is not a id-expression anyway

Assume the declared non-static member in the closure type for the captured entity *this by copy is instance_a_, the rule obviously cannot apply to (*this).a = 1; to transform it to instance_a_.a = 1;.

Suggested Resolution

Every expression E within the compound-statement of a lambda-expression that denotes an entity captured by copy and the entity is odr-used ([basic.def.odr]) by E is replaced by the corresponding unnamed data member of the closure type.

xmh0511 commented 9 months ago

Seems to be covered by

If this is captured by copy, each expression that odr-uses this is transformed to instead refer to the corresponding unnamed data member of the closure type.