Topic: unnnamed classes/structs and anonymous unions
Author: user name <user182@MailAndNews.com>
Date: Sat, 28 Apr 2001 10:53:48 GMT Raw View
Recently, with the help of some friends I tried to compile the
following code on BorlandC++ Builder 5.5, VC++ 6, Comeau web interface,
and two versions of gcc.
struct X
{
union
{
int w;
struct
{
short h;
short l;
};
};
};
int main (){}
Comeau won't compile it.
I was told that VC++ wont either.
I was told that Borland C++ Builder would.
I had one person tell me that gcc would
not compile the code (I forget the version
as I am recalling this from memory).
g++ (2.95.2-6 19991024 (cygwin experimental))
with the -ansi switch compiles without complaint.
With the -pedantic switch it adds a warning
"warning: ISO C++ prohibits anonymous structs"
I am skeptical of that warning becuase:
- Unnamed classes are mentioned in 3.5 /4
- 9.4.2 /5 discusses the possibility of unnamed
classes within other unnamed classes.
- As a follower of this newsgroup, I have seen
it's respected members define unnamed classes.
I could not find the language that describes my code
fragment as ill-formed.
I tried to search in all of the relevant sections, the
only measure that I did not take was to examine the grammar.
Could someone tell me if that code fragment is well formed
or not ?
It would be appreciated, thanks.
---
[ 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 ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sun, 29 Apr 2001 13:21:50 GMT Raw View
user name wrote:
>
> Recently, with the help of some friends I tried to compile the
> following code on BorlandC++ Builder 5.5, VC++ 6, Comeau web interface,
> and two versions of gcc.
>
> struct X
> {
> union
> {
> int w;
> struct
> {
> short h;
> short l;
> };
> };
> };
...
> g++ (2.95.2-6 19991024 (cygwin experimental))
> with the -ansi switch compiles without complaint.
> With the -pedantic switch it adds a warning
> "warning: ISO C++ prohibits anonymous structs"
>
> I am skeptical of that warning becuase:
>
> - Unnamed classes are mentioned in 3.5 /4
You're confusing anonymous classes with unnamed classes. The C++
standard allows unnamed classes. It does not define "anonymous" classes,
but by analogy with unions it's quite clear what an anonymous class
would be, and the standard doesn't support them (for reasons to be
explained below). As you've found out, some compilers support them
anyway, as an extension to the language.
> - 9.4.2 /5 discusses the possibility of unnamed
> classes within other unnamed classes.
>
> - As a follower of this newsgroup, I have seen
> it's respected members define unnamed classes.
Yes, with syntax similar to the following:
struct {
short h;
short l;
} this_pair;
The structure type is unnamed, but the declaration defines a named
object.
I don't blame you for your confusion. The relevant section of the
standard is very heavy with jargon, and it takes a lot of thought to see
why it's relevant. I would never have realized that it was relevant if
someone else hadn't pointed it out to me first.
The problem with your code is that the declaration containing 'h' and
'l' doesn't introduce any names into the program. You might think that
your declaration introduces 'h' and 'l' into your program. For
corresponding code using an 'enum' or a 'union', you'd be right. You
don't need an lvalue of enumeration type to access the associated
enumerators. Ordinarily, you do need an lvalue of a union type to access
the members of a union, but the standard has made a special case for
members of anonymous unions in section 9.5p2. However, no such exemption
has been made for classes. You always need an lvalue of class type in
order to access any of that class's non-static members.
Since your declaration does not define an object of the structure type,
and provides no type name that can be used to declare some other object
to be of that type, you can never form an lvalue of that type.
Therefore, you can't access any of it's members. Therefore, the
declaration does not introduce any names into the program, and is in
violation of section 7, paragraph 3:
"In a _simple-declaration_, the optional _init-declarator-list_ can be
omitted only when declaring a class (clause 9) or enumeration (7.2),
that is, when the _decl-specifier-seq_ contains either a
_class-specifier_, an _elaborated-type-specifier_ with a
_class-key(9.1), or an _enum-specifier_. In these cases and whenever a
_class-specifier_ or _enum-specifier_ is present in the
_decl-specifier-seq_, the identifiers in these specifiers are among the
names being declared by the declaration (as _class-names_, or
_enumerators_, depending on the syntax). In such cases, and except for
the declaration of an unnamed bit-field (9.6), the _decl-specifier-seq_
shall introduce one or more names into the program, or shall redeclare a
name introduced by a previous declaration."
---
[ 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 ]
Author: Kaleb Pederson <kibab@icehouse.net>
Date: Sun, 29 Apr 2001 13:22:08 GMT Raw View
In gcc-2.95.3, with the -ansi switch I get the following:
[kibab@localhost test]$ gcc -ansi anon_union.c -o anon_union
anon_union.c:11: warning: unnamed struct/union that defines no instances
anon_union.c:12: warning: unnamed struct/union that defines no instances
with the -pedantic switch I get the following:
[kibab@localhost test]$ gcc -pedantic anon_union.c -o anon_union
anon_union.c:11: warning: ANSI C forbids member declarations with no members
anon_union.c:11: warning: unnamed struct/union that defines no instances
anon_union.c:12: warning: ANSI C forbids member declarations with no members
anon_union.c:12: warning: unnamed struct/union that defines no instances
anon_union.c:13: warning: struct has no members
I'm not terribly familiar with the standard, but I have to agree with you
in that this seems correct.
--Kaleb
user name wrote:
> Recently, with the help of some friends I tried to compile the
> following code on BorlandC++ Builder 5.5, VC++ 6, Comeau web interface,
> and two versions of gcc.
>
> struct X
> {
> union
> {
> int w;
> struct
> {
> short h;
> short l;
> };
> };
> };
>
> int main (){}
>
> <snipped>
>> I had one person tell me that gcc would
> not compile the code (I forget the version
> as I am recalling this from memory).
>
> g++ (2.95.2-6 19991024 (cygwin experimental))
> with the -ansi switch compiles without complaint.
> With the -pedantic switch it adds a warning
> "warning: ISO C++ prohibits anonymous structs"
---
[ 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 ]