[Issue 18855] New: Behavior of Anonymous Union is Undocumented

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun May 13 09:40:26 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18855

          Issue ID: 18855
           Summary: Behavior of Anonymous Union is Undocumented
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P1
         Component: dlang.org
          Assignee: nobody at puremagic.com
          Reporter: madric at gmail.com

In the DLang specification here:
https://dlang.org/spec/struct.html#AnonUnionDeclaration

It is said that anonymous unions can be declared, but the ramifications of this
are never clarified. Those coming from C++ will suspect that it makes the
anonymous union members members of the containing object, but for those coming
from Java or another language, this behaviour will seem unexplained.

Example below:
```
void main()
{
  import std.stdio;
  struct Bob {
    int a;
    union {  // Anonymous union.
      int b;
      int c;
    }
    union Cat {
      int d;
      int e;
    }
    Cat cat;
  }

  Bob bob = Bob(1, 2);
  assert(bob.a == 1);
  assert(bob.b == 2);
  assert(bob.c == 2);
  bob.cat = Bob.Cat(3);
  // assert(bob.d == 3); // ERROR, Bob has no property 'd'.
  assert(bob.cat.d == 3);  // OK
}
```

--


More information about the Digitalmars-d-bugs mailing list