Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Welcome to the universe of C++! For your question, the answer lay in a concept called 'Point-of-declaration'.</p> <pre><code>&gt;&gt;int x = 1; &gt;&gt;{ int x = x; } // garbage value of x </code></pre> <p>From Section:-3.3.1.1 (C++ Standard Draft) The point of declaration for a name is immediately after its complete declarator and before its initializer (if any), except as noted below.</p> <pre><code>int x = 12; { int x = x; } </code></pre> <p>Here; the 'operator =' is the initializer. You can say that the point-of-declaration for 'x' is not yet reached, so the value of 'x' is indeterminate.</p> <pre><code>&gt;&gt;int const arr = 3; &gt;&gt;{ int arr[arr]; } // i am told this is perfectly valid and declares an array of 3 ints !! </code></pre> <p>Why? From Section:-3.3.1.4 (C++ Standard Draft) A nonlocal name remains visible up to the point of declaration of the local name that hides it. Here the point of declaration is reached at ';' character. So the earlier visible value of 'arr' is used i.e. = 3.</p> <p>Also, you may wish to know that the following is valid :-</p> <pre><code>const int e = 2; { enum { e = e }; } // enum e = 2 </code></pre> <p>From Section:-Chapter-3.3.1.4 (C++ Standard Draft):- The point of declaration for an enumerator is immediately after its enumerator-definition.</p> <p>But, don't do this</p> <pre><code>const int Spades = 1, Clubs = 2, Hearts = 3, Diamonds = 4; enum Suits { Spades = Spades, // error Clubs, // error Hearts, // error Diamonds // error }; </code></pre> <p>Why? Because enumerators are exported to the enclosing scope of the enumeration. In the above example, the enumerators Spades, Clubs, Hearts, and Diamonds are declared. Because the enumerators are exported to the enclosing scope, they are considered to have global scope. The identifiers in the example are already defined in global scope. So its an error.</p> <p>For additional details and pitfalls ( :-) ), read-up on section 3.3 'Declarative regions and scopes' from the Draft C++ Standard, if interested you can get hold of the pdf from <a href="http://www.research.att.com/~bs/SC22-N-4411.pdf" rel="nofollow noreferrer">here</a>(<a href="http://www.research.att.com/~bs/SC22-N-4411.pdf" rel="nofollow noreferrer">http://www.research.att.com/~bs/SC22-N-4411.pdf</a>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload