[Issue 15258] New: Anonymous const union members don't allow for initialization

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Oct 29 11:29:15 PDT 2015


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

          Issue ID: 15258
           Summary: Anonymous const union members don't allow for
                    initialization
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: adam at statystyka.net

The following code doesn't compile:

class A {
    this(const char* result)
    {
        this.result = result;
    }

    private:
        union{
            const char** results;
            const char* result;
        }
}

Compilation fails with "constructor app.A.this missing initializer for const
field results". I know I can make it compile if I remove the const qualifier in
the union definition, but this is a poor walkaround. 

There are two better walkarounds:

1. Change the constructor body into 
{
    this.results = null;
    this.result = result;
}

2. Make the union named:

class A {
    this(const char* result)
    {
        this.u.result = result;
    }

    private:
        union U{
            const char** results;
            const char* result;
        }
        U u;
}

--


More information about the Digitalmars-d-bugs mailing list