New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[expr.prim.lambda.capture] Which entity does the id-expression captured by reference refer to after evaluating the lambda-expression? #4619
Comments
You mean that the context in which variable
Where do you want to put the «otherwise» part?
Why the presence of the corresponding NSDM is relevant when such id-expression is never transformed to member access? |
Since the wording is lack for the case of the reference capture when the prvalue(lambda-expression) is evaluated, it should clearly specify what the id-expression will denote(captured by reference) . [expr.prim.lambda#capture-15] totally talks about the case of captured by copy, regardless of simple-capture or init-capture
Maybe
It could tie the entity named by the id-expression to the id-expression that appears in the call operator function's body of the result object of the lambda-expression.
Since [expr.prim.lambda#capture-12] says
The implementation can adopt the suggestion to declare an NSDM for this purpose as long as the member is of literal type. So, in this case, the standard should clarify how the member will be initialized when the lambda-expression is evaluated and how the id-expression in the function body will be transformed. |
I still don't get why the description of how an id-expression is evaluated in capture-by-reference case should depend on the presence of the corresponding NSDM. |
Because the standard does not forbid the implementation to declare NSDM for this purpose(capture-by-reference case), since it can declare the member, the use of the id-expression should transform to the corresponding unnamed data member of the closure type; as the same manner as the following:
Otherwise, how is the meaning of adding the unnamed NSDM for In contrast, if the implementation adopts the suggestion that does not declare such an NSDM, we should clarify what the id-expression denotes in every result object. Please consider the following example: void fun(){
int a;
static auto f = [&a](){
a = 10;
};
}
int main(){
fun();
fun();
} Even though the |
What is «this purpose» here?
I don't see a logical connection.
In the capture-by-copy case, I see the reason to transform an id-expression to class member access. In the capture-by-reference case, I see none.
It is implementation's business/details why it adds a NSDM.
When adding the corresponding NSDM, I think an implementation is no allowed to change the observable behavior other than by changing the size of the closure type. So I don't think there is a need to describe non-observable things such as the initializer of this NSDM.
I understand where the issue is. I don't see the connection with NSDMs. |
Please note the [expr.prim.lambda#capture-12]
It appears to me that the standard supplies two options to implement the
If the implementation takes the latter option, namely add the unnamed NSDM for implementing |
I'm seeing the following problem being described: The note in [expr.prim.lambda.capture] p16 lacks normative wording to base it on. In particular, it should be made clear that the evaluation of a lambda-expression causes the reference binding of entities captured by reference, and that (later) evaluation of an id-expression that names an entity captured by reference uses that reference binding (which might refer to an out-of-lifetime object). |
Yes, I exactly mean that. The standard introduces two options to implement |
I don't think we need to differentiate those two options as long as we say what the effects are. |
I don't know how the concrete normative rule would be, maybe such two options can be unified described by that rule. However, in my humble opinion, if we adopt the latter option(namely, introduce an additional unnamed NSDM), the use of the id-expression in the compound-statement of the lambda-expression will be replaced by using that member. For the above example, the use of the id-expression this->unnamed_member_for_a = 1; This means the member should be initialized in order to refer to the entity captured by reference when the lambda-expression is evaluated to initialize its result object. Instead, the former option does not have the additional member, hence there's no member to be initialized and no transformation for the id-expression, where I think the difference is here. Specifically, for the former option, we should state the rule to make the id-expression clearly refer to the captured entity. For the latter option, we should state the rule to make the additional member clearly refer to the captured entity. |
It is underspecified about which entity the id-expression captured by reference refers to when the lambda-expression is evaluated. For instance:
In major implementations, each invocation of the function
fun
will make that the id-expressiona
at#3
denotes the different object. In other words, the evaluation of the prvalue lambda-expression will initialize its result object which isf
, however, the standard does not specify what object the id-expressiona
will refer to in every result function objectf
.The rule for an entity that is captured by reference is defined as the following:
It does not explicitly specify the
otherwise
case that there's no additional unnamed non-static data member to be declared. So, we may unclear know what entity the id-expression refers to when the lambda-expression is evaluated, where the id-expression is captured by reference. As far as I know, the following rule does not apply to the above example as well.Since in the simple-capture
&a
wherea
is not a reference. Instead, it's an object.In addition, even though the unnamed non-static data member is declared for such capture(captured by reference), I think the following rule is still not suitable for that case
Also, [expr.prim.lambda#capture-15] does not specify how the initialization would be for the unnamed non-static data member declared for the case captured by reference.
Generally, the standard has no wording about what the id-expression would denote if it's reference capture in two cases(has/has no the corresponding unnamed non-static data member).
The text was updated successfully, but these errors were encountered: