www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18855] New: Behavior of Anonymous Union is Undocumented


          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 puremagic.com
          Reporter: madric gmail.com

In the DLang specification here:

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

May 13 2018