Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!snorkelwacker!think!bbn!husc6!contact!ileaf!io!jar From: jar@io.UUCP (Jim Roskind x5570) Newsgroups: comp.lang.c++ Subject: identifiers enter scope: language lawyers Message-ID: <1372@io.UUCP> Date: 14 Jan 90 22:04:22 GMT Organization: Interleaf Inc, Cambridge, MA Lines: 75 In article by ark@alice.UUCP (Andrew Koenig) >In article <1794@thumper.bellcore.com>, clayton@thumper.bellcore.com (R. Clayton) writes: >> ... >One might think the following might work too: > extern char *home_dir, *path, *file; > Pathname home = home_dir; > { > Pathname file = home + path + file; > FILE *fp = file->open(); > // ... > } >with `file' in `home + path + file' referring to the `file' in >the enclosing scope. Unfortunately, the ANSI C standard decrees >that a variable is defined from the instant its name is uttered >in its declaration and C++ goes along with that. The language of the dpANSI C Standard is very subtle, and (as I recall) the precise statement is that the identifier is defined from the instant the "declarator is complete". In C, this point is nicely marked by either a ';' (end of declaration), '=' (start of initializer), or ',' (start of next declarator). In C++, this location is unclear to a typical LR1 parser, when a parenthesized initializer is used. Although this detail has not (to my knowledge) been addressed in the C++ Reference Manual, I believe there should be a subtle distinction for C++ in this area. Specifically, I believe that in C++ the identifier should be placed into the scope at the end of parenthesized initializer (if any), and otherwise at the end of the declarator (as in ANSI C). Assuming this resolution is adopted, the following slightly obscure code should work: extern char *home_dir, *path, *file; Pathname home = home_dir; { Pathname file ( home + path + file ) ; FILE *fp = file->open(); // ... } I am only guessing, but I assume some C++ parsers would accept this already with the semantics that are desired. To be specific:: const int t=5; void main() { int t(t+1); // should initialize this local t to 6 } By the way, I am NOT advocating use of such cryptic code, only standardization of its meaning. The following very cryptic code can also be nicely disambiguated using the above rule: typedef int * T1; int *pi1; void main() { int (*T1)(T1); //redeclares T1 "pointer to function taking int *" void * pv = &pv; // valid ANSI C code int *pi1(pi1); //redeclare local pi1 with initial value ::pi1 } >-- > --Andrew Koenig > ark@europa.att.com Jim Roskind Independent consultant (407)729-4348 jar@ileaf.com