cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[basic.def.odr] p10 The intent is not clear #389

Open xmh0511 opened 1 year ago

xmh0511 commented 1 year ago

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

[basic.def.odr] p10

A local entity is odr-usable in a scope if:

  • [...]
  • for each intervening scope ([basic.scope.scope]) between the point at which the entity is introduced and the scope (where *this is considered to be introduced within the innermost enclosing class or non-lambda function definition scope), either:
    • the intervening scope is a block scope, or
    • the intervening scope is the function parameter scope of a lambda-expression that has a simple-capture naming the entity or has a capture-default, and the block scope of the lambda-expression is also an intervening scope.

Consider a variant of the example below the rule

void fun(){
    int n = 0;
   [=](int k = n) { int c = n;};    
}

Now, both the parameter scope and block scope of the lambda-expression are intervening scopes, so the n can appear in the function parameter scope of the lambda? It seems not to be the intent of the rule.

Suggested Resolution

for each intervening scope ([basic.scope.scope]) between the point at which the entity is introduced and the scope (where *this is considered to be introduced within the innermost enclosing class or non-lambda function definition scope), ~either~:

  • the intervening scope is a block scope, and if the scope is a block scope of a lambda-expression, the lambda-expression shall have a simple-capture naming the entity or have a capture-default and the function parameter scope of the lambda-expression is also an intervening scope.

This avoids the misreading in the original wording when both the function parameter scope and block scope of the lambda are intervening scopes. This also avoids talking about the intervening scope from different "points"/"contexts" that are needed in the original wording.

frederick-vs-ja commented 1 year ago

Related to cplusplus/draft#5406. "Function definition scope" needs to be either defined or eliminated.

jensmaurer commented 1 year ago

The definition is for "odr-usable in a scope". How is the block scope of the lambda an intervening scope for the "n" appearing in the default argument?