From: Baptiste L. <gai...@fr...> - 2002-11-20 09:11:44
|
Thanks. Do you know if in the following declarations: 1) the temporary is visible after the if statement (I don't think so, but...) 2) if the scope of the temporary declared in the condition is the same as the scope of the temporary declared in the then/else/iteration statement if ( int x = 3, y = x ) { int x; // is it legal ? (different scope ?) } // is x visible here ? while ( int x = 3, y = x ) { int x; // is it legal ? (different scope ?) } // is x visible here ? A tricky one to deal with will be the 'for' statement, since all compilers do not follow the standard (and gcc is even trickier since it allows both non-conformant usage (with a warning) and conformant usage... There is also the matter of the using declaration, which promise to be 'fun' to deal with... Baptiste. --- Original Message ----- From: "Rohith Rajagopal" <man...@ho...> To: <cpp...@li...> Sent: Wednesday, November 20, 2002 6:23 AM Subject: [Cpptool-develop] Abt Sven's question on scope > This is what the ANSI C++ std has to say: > http://www.comnets.rwth-aachen.de/doc/c++std/basic.html > The example below will probably answer the question. > > 1 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. In general, each particular > name is valid only within some possibly discontiguous portion of program > text called its scope. To determine the scope of a declaration, it is > sometimes convenient to refer to the potential scope of a declaration. The > scope of a declaration is the same as its potential scope unless the > potential scope contains another declaration of the same name. In that > case, the potential scope of the declaration in the inner (contained) > declarative region is excluded from the scope of the declaration in the > outer (containing) declarative region. > > 2 [Example: in > 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. ] |