This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/42875] New: gcc disallows named initializers for anonymous unions
- From: "mat at lcs dot mit dot edu" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 26 Jan 2010 16:03:39 -0000
- Subject: [Bug c/42875] New: gcc disallows named initializers for anonymous unions
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
gcc-4.4.3 lets you statically initialize named fields, and lets you assign to
anonymous union members, but you cannot statically initialize a named member of
an anonymous union. This inconsistency between assignment and initialization
is surprising, and breaks some code I am porting from an EDG-based compiler to
gcc-4.4.3.
Let me emphasize that I don't claim this violates any official standard, since
these are language extensions. But it does violate the "principle of least
surprise", since one would expect that the "assignment" and "initialization"
field namespaces would be the same, as with EDG.
Here is an example:
// Union with anonymous member.
union my_union
{
int my_int;
struct
{
short a, b;
};
};
// Works: gcc lets you initialize named fields.
union my_union
initialize_int()
{
union my_union x = { .my_int = 3 };
return x;
}
// Works: gcc lets you assign to anonymous union members.
union my_union
assign_anonymous_union(union my_union x)
{
x.a = 3;
return x;
}
// Fails: gcc does not let you initialize named members in anonymous unions.
union my_union
initialize_anonymous_union()
{
// Does not compile,
//
// foo.c: In function ?initialize_anonymous_union?:
// foo.c:33: error: unknown field ?a? specified in initializer
union my_union x = { .a = 3 };
return x;
}
--
Summary: gcc disallows named initializers for anonymous unions
Product: gcc
Version: 4.4.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: mat at lcs dot mit dot edu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42875