This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/42875] New: gcc disallows named initializers for anonymous unions


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]