www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15258] New: Anonymous const union members don't allow for

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 puremagic.com
          Reporter: adam 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;
}

--
Oct 29 2015