Bug 42875 - gcc disallows named initializers for anonymous unions
Summary: gcc disallows named initializers for anonymous unions
Status: RESOLVED DUPLICATE of bug 10676
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.4.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-26 16:03 UTC by Mat Hostetter
Modified: 2010-01-26 16:25 UTC (History)
11 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mat Hostetter 2010-01-26 16:03:39 UTC
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;
}
Comment 1 Mat Hostetter 2010-01-26 16:25:54 UTC


*** This bug has been marked as a duplicate of 10676 ***