cplusplus / cplusplus/draft

[expr.prim.lambda.capture] Which entity does the id-expression captured by reference refer to after evaluating the lambda-expression?

Open
#4,619 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

[expr.prim.lambda#capture-15] When the lambda-expression is evaluated, the entities that are captured by copy are used to direct-initialize each corresponding non-static data member of the resulting closure object, and the non-static data members corresponding to the init-captures are initialized as indicated by the corresponding initializer (which may be copy- or direct-initialization). (For array members, the array elements are direct-initialized in increasing subscript order.) These initializations are performed in the (unspecified) order in which the non-static data members are declared.

It is underspecified about which entity the id-expression captured by reference refers to when the lambda-expression is evaluated. For instance:

void fun(){
  int a;
 auto f = [&a](){
    a = 1; //#3
 };
}
int main(){
  fun(); //#1
  fun(); //#2
}

In major implementations, each invocation of the function fun will make that the id-expression a at #3 denotes the different object. In other words, the evaluation of the prvalue lambda-expression will initialize its result object which is f, however, the standard does not specify what object the id-expression a will refer to in every result function object f.

The rule for an entity that is captured by reference is defined as the following:

[expr.prim.lambda#capture-12] An entity is captured by reference if it is implicitly or explicitly captured but not captured by copy. It is unspecified whether additional unnamed non-static data members are declared in the closure type for entities captured by reference. If declared, such non-static data members shall be of literal type.

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.

[expr.prim.lambda#capture-13] An id-expression within the compound-statement of a lambda-expression that is an odr-use of a reference captured by reference refers to the entity to which the captured reference is bound and not to the captured reference.

Since in the simple-capture &a where a 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

[expr.prim.lambda#capture-11] Every id-expression within the compound-statement of a lambda-expression that is an odr-use of an entity captured by copy is transformed into an access to the corresponding unnamed data member of the closure type.

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).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the cited [expr.prim.lambda#capture-12], -13, and -15 wording alongside the provided lambda example. Determine how the id-expression should denote an entity for reference captures with and without an unnamed data member, and what initialization wording is required; done means both cases are specified without ambiguity.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.