Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!sri-spam!mordor!lll-lcc!pyramid!pesnta!valid!roger From: roger@valid.UUCP Newsgroups: comp.lang.c++ Subject: Re: Structures want anonymity, too. Message-ID: <1062@valid.UUCP> Date: Wed, 11-Mar-87 18:16:54 EST Article-I.D.: valid.1062 Posted: Wed Mar 11 18:16:54 1987 Date-Received: Fri, 13-Mar-87 03:43:49 EST References: <165700008@uiucdcsb> Organization: Valid Logic, San Jose, CA Lines: 84 We (Valid Logic Systems, Inc.) have modified our 1.1 C++ translator to allow anonymous structures as well as anonymous unions. Our primary motivation for this enhancement was for use with our Pascal-to-C++ translator that we are developing. In Pascal there is a construct known as a "variant record" that takes the following form (from Jensen & Wirth, 2nd Edition, pp. 42 & 45): ::= record end ::= | ; | ::= { ; } ::= {, } : | ::= case of {; } ::= : ( ) | ::= {, } ::= ::= : | Thus, one can have types such as: variant_record_example = record a: integer; case boolean of FALSE: (b, c, d: integer; e: real); TRUE: (f: array [0 .. 10] of char; g: random_type_name) end; var q: variant_record_example; begin q.a := 1; q.b := 2; q.e := 3.0; where all of the names of the various variant and non-variant fields are in the same scope. The "equivalent" type in C would look like: typedef struct { int a; union { struct { int b, c, d; float e; } _pointless_name_1; struct { char f[11]; random_type_name g; } _pointless_name_2; } _pointless_name_3; } variant_record_example; variant_record_example q; q.a = 1; q._pointless_name_3._pointless_name_1.b = 2; q._pointless_name_3._pointless_name_1.e = 3; Standard C++ allows you to eliminate _pointless_name_3, but still requires _pointless_name_1 and _pointless_name_2. Our extension allows you to declare this type as: typedef struct { int a; union { struct { int b, c, d; float e; }; struct { char f[11]; random_type_name g; }; }; } variant_record_example; Once you have anonymous structures AND unions you begin to see their more abstract function, which is to group a collection of variables for storage allocation purposes; a struct allocates them in series and a union allocates them in parallel. Naming a struct or union then becomes merely a scope issue, allowing you to create many simultaneous, independent name spaces for variables. I have discussed this issue with Bjarne and he said that he is considering adopting this change in the standard. Others who believe it is a useful generalization might want to drop Bjarne a line to that effect. Roger H. Scott Valid Logic Systems, Inc. 2820 Orchard Parkway San Jose, CA 95134 (408) 432-9400 x2484