Bug 15258 – Anonymous const union members don't allow for initialization
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-10-29T18:29:00Z
Last change time
2016-10-01T11:46:50Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
adam
Comments
Comment #0 by adam — 2015-10-29T18:29:15Z
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;
}