Topic: Anonymous struct in an union


Author: Pierre Baillargeon <pb@artquest.net>
Date: 2000/11/28
Raw View
Is the following code legal? The point being questionned in the use of
an anonymous structure in an union, and its initialization in the
constructor. A compiler I'm starting to use does not see the structure
members and affirms that the variable is not defined.

union A
{
   int foo;
   struct { char a; char b; char c; char d; };

   A() : a (0), b (0), c(0), d(0) {}
};


The compiler complains that a, b, c and d are unknown and thus cannot be
initialized in the constructor. Another compiler accepts the code.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Andrei Iltchenko <Andrei.Iltchenko@openmarket.com>
Date: 2000/11/28
Raw View
Pierre Baillargeon wrote:

> Is the following code legal? The point being questionned in the use of
> an anonymous structure in an union, and its initialization in the
> constructor. A compiler I'm starting to use does not see the structure
> members and affirms that the variable is not defined.
>
> union A
> {
>    int foo;
>    struct { char a; char b; char c; char d; };
>
>    A() : a (0), b (0), c(0), d(0) {}
> };
>
> The compiler complains that a, b, c and d are unknown and thus cannot be
> initialized in the constructor. Another compiler accepts the code.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
> [ Note that the FAQ URL has changed!  Please update your bookmarks.     ]

The code is illegal indeed. The standard doesn't define the notion
of an anonymous structure (there's a notion of an anonymous union though).

The construct
    struct { char a; char b; char c; char d; };
is just an unnamed class.

And the names of its data members are not considered defined in the scope in

which the unnamed class is itself defined as is the case for anonymous
unions.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/11/28
Raw View

Andrei Iltchenko wrote:
>
> The construct
>     struct { char a; char b; char c; char d; };
> is just an unnamed class.
>

Which is illegal.  A struct declaration must either
define a type:
 struct s { char a; char b; char c; char d; };
or be used to declare an object:
 struct { char a; char b; char c; char d; } o;
or be used to make a typedef:
 typedef struct { char a; char b; char c; char d; } t;

A declarator must (with small exception) introduce a name
into the program.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/11/28
Raw View
In article <3A23D829.1D3F57E2@artquest.net>,
Pierre Baillargeon  <pb@artquest.net> wrote:
>Is the following code legal? The point being questionned in the use of
>an anonymous structure in an union, and its initialization in the
>constructor. A compiler I'm starting to use does not see the structure
>members and affirms that the variable is not defined.
>
>union A
>{
>   int foo;
>   struct { char a; char b; char c; char d; };
>
>   A() : a (0), b (0), c(0), d(0) {}
>};
>
>The compiler complains that a, b, c and d are unknown and thus cannot be
>initialized in the constructor. Another compiler accepts the code.

It's not legal.  It's an extension in some compilers though.

- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/11/29
Raw View

Pierre Baillargeon wrote:
>
> Is the following code legal?

No.  There are no such things as anonymous structs in C++ (in
unions or elsewhere.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 2000/11/29
Raw View
In article <3A23D829.1D3F57E2@artquest.net>, Pierre Baillargeon
<pb@artquest.net> writes
>Is the following code legal? The point being questionned in the use of
>an anonymous structure in an union, and its initialization in the
>constructor. A compiler I'm starting to use does not see the structure
>members and affirms that the variable is not defined.

While C++ supports anonymous unions (by allowing direct use of the union
member) in class scope, it does not support an equivalent concept for
struct or class.


Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]