Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret the "point of declaration" for "const int i=2; { int i[i]; }" - an example from the C++ standard?

I am studying the C++ standard to understand order of operations, expressions, statements, and side effects.

A related issue is the "point of declaration" for a name. In section §3.3.2.1 of the C++11 standard, the standard states:

The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any)...

The following paragraph adds a note with an example:

Note: a name from an outer scope remains visible up to the point of declaration of the name that hides it.

... with the example

const int i = 2;
{ int i[i]; }

and the comment states that this code sample "declares a block-scope array of two integers."

I would like to be able to look at the sample code and, by applying the definition of "point of declaration" along with the note regarding visibility of a name from an outer scope, be able to look at that code and come to the logical conclusion, myself, that it is well-formed code that does what it appears to do.

However, I cannot understand how to look at this code and arrive at this conclusion, based on these definitions and rules.

Reading from left-to-right, the declaration of the i[] array seems to appear before the use of i from the outer scope, so the meaning of "remains visible up to the point of declaration" cannot mean in a reading-left-to-right sense.

"Up to the point of declaration" must mean something along the lines of an "order of operations" sense.

What definition or rule in the C++ standard dictates that in an array declaration, the "point of declaration" of the array name occurs "after" the expression inside the square brackets? What exactly is meant by "after" in this case?

like image 218
Dan Nissenbaum Avatar asked Dec 06 '12 19:12

Dan Nissenbaum


2 Answers

"complete declarator" --> Declarator includes the [] and their contents. That is, the complete declarator above is i[i], so it gets declared only after that.

like image 132
tjltjl Avatar answered Nov 09 '22 01:11

tjltjl


The point of declaration is immediately after the complete declarator. Therefore, the outer i is not yet hidden inside the declarator itself.

like image 43
phoog Avatar answered Nov 09 '22 02:11

phoog