Names denoted the same entity

2019-02-25 10:28发布

The following definition of declarative region:

Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity.

We have example into the spec below:

int j = 24;
int main() {
    int i = j, j;
    j = 42;
}

the identifier j is declared twice as a name (and used twice). The declarative region of the first j includes the entire example. The potential scope of the first j begins immediately after that j and extends to the end of the program, but its (actual) scope excludes the text between the , and the }. The declarative region of the second declaration of j (the j immediately before the semicolon) includes all the text between { and }, but its potential scope excludes the declaration of i. The scope of the second declaration of j is the same as its potential scope.

It's unclear how to determine declarative region for arbitrary name. At least I can't find this into the standard.

1条回答
2楼-- · 2019-02-25 10:58

The potential scope of a variable declared at the file scope (i.e., not inside a namespace, class, or function) is from the point at which the variable is declared until the end of file. The potential scope of a variable declared inside a function is from the point at which the variable is declared until the close brace inside of which the variable was declared.

The actual scope of a variable can be smaller than the potential scope if a new variable of the same name is declared at some inner scope. This is called shadowing.

// The following introduces the file scope variable j.
// The potential scope for this variable is from here to the end of file.
int j = 42; 

// The following introduces the file scope variable k.
int k = 0;

// Note the parameter 'j'. This shadows the file scope 'j'.
void foo (int j) 
{
    std::cout << j << '\n'; // Prints the value of the passed argument.
    std::cout << k << '\n'; // Prints the value of the file scope k.
}
// The parameter j is out of scope. j once again refers to the file scope j.


void bar ()
{
    std::cout << j << '\n'; // Prints the value of the file scope j.
    std::cout << k << '\n'; // Prints the value of the file scope k.

    // Declare k at function variable, shadowing the file scope k.
    int k = 1; 
    std::cout << k << '\n'; // Prints the value of the function scope k.

    // This new k in the following for loop shadows the function scope k.
    for (int k = 0; k < j; ++k) { 
        std::cout << k << '\n'; // Prints the value of the loop scope k.
    }
    // Loop scope k is now out of scope. k now refers to the function scope k.

    std::cout << k << '\n'; // Prints the function scope k.
}
// Function scope k is out of scope. k now refers to the file scope k.
查看更多
登录 后发表回答