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; }
Comment #1 by adam — 2015-10-29T18:30:14Z
The issue is also discussed at Stackoverflow: http://stackoverflow.com/questions/33415489
Comment #2 by k.hara.pg — 2015-10-30T06:33:43Z
Comment #3 by github-bugzilla — 2016-05-07T10:42:08Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/5175be50ba15738cf0f90ed4136f9a0484ecd352 fix Issue 15258 - Anonymous const union members don't allow for initialization https://github.com/dlang/dmd/commit/87a802f7aa6d91c09cfd7604f6ea77c7d6fd477e Merge pull request #5242 from 9rnsr/fix15258 Issue 15258 - Anonymous const union members don't allow for initialization
Comment #4 by github-bugzilla — 2016-10-01T11:46:50Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/5175be50ba15738cf0f90ed4136f9a0484ecd352 fix Issue 15258 - Anonymous const union members don't allow for initialization https://github.com/dlang/dmd/commit/87a802f7aa6d91c09cfd7604f6ea77c7d6fd477e Merge pull request #5242 from 9rnsr/fix15258