what are name lookup rules within the hypothetical declaration that required in some contexts

Viewed 174

Issues 1:

int v = 10;
auto f = [v = v](){};

Consider the above code,some rules mentioned in the standard will work for it.

[expr.prim.lambda.capture#6]

An init-capture behaves as if it declares and explicitly captures a variable of the form “auto init-capture ;” whose declarative region is the lambda-expression's compound-statement.

The quote means that auto v = v; would be declared in the lambda's compound-statement as if it like this:

//hypothetical code
int v = 10;
auto f = [](){
  auto v = v;  //the second `v` is the first `v` and will be initialized by an indetermined value.
}; 

So,For the hypothetical declaration auto v = v;,I think it will conflict with the following rules.

[basic.scope.pdecl#1]

The point of declaration for a name is immediately after its complete declarator and before its initializer (if any) [ Example:
unsigned char x = 12;
{ unsigned char x = x; }
Here the second x is initialized with its own (indeterminate) value.  — end example ]

Issue 2:

void func(int v){}  
int v = 0;  
func(v);   

We know the initialization of the function's parameter will occur within the context of the calling function and it will be copy-initialization.We ignore the quotes in the standard,because they are too much.So the copy-initialization of parameter will appear in somewhere within the context of the calling function as if it likes such form:

//hypothetical code
int v = v; // initialization of a parameter that occured in somewhere within context of calling function 

The same issue like issue 1,how to interpret these issues when the name of the declarator-id is the same as that of the id-expression that within a initializer when they occured in these hypothetical copy-initialization contexts.Does these hypothetical copy-initializations contradict with the rule [basic.scope.pdecl#1]

Question:

I wonder that Is the lookup for the id-expression within initializer preceded than the hypothetical declaration occures? why do I think so? because, as the opposite, During the usual declaration, the lookup for initializer occures after the declarator immediately complete, as said in [basic.scope.pdecl#1], It means the declarator-id would hide the id-expression that within the initializer if they are the same name.

Is there any quote in the standard interpret this?

1 Answers

initializer in [basic.scope.pdecl] refers to an element of a grammar rule, that is used by the parser. It is defined in [dlc.init] and it appears in the init-declarator grammar rule.

For the case of the lambda capture, the standard specifies that it is equivalent to an init-declarator but whose declarative region is the lambda-expression's compound-statement[...] that is, the capture name is not in the scope of the initializer.

The term initialization does not refer to the grammar rule referred by the term initilizer. initialization is an expression evaluation that initialize a variable, this is not a grammar rule. The rules which involve those two terms cannot conflict because they are not part of the same phase of the compilation->execution process:

1) The grammar rules for initializer, declarator, or init-caputure, etc... are used to determine to which object refers an id-expression (a name within an expression) that appears within an initializer. An id-expression is a glvalue. A glvalue referes to an object, and it has no name.

2) Then those glvalues are parts of the expression that perform initialization. When this initialization takes place, names are irrelevant.

NB: In the standard, many rules referrers to grammar rules and you should take care of the formatting to recognize them, because their name has no more sense in itself than the name of a variables that appear inside mathematical equations.

Related