Confusion/trying to understand CTFE keywords

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Jun 5 11:30:50 UTC 2018


On Tuesday, June 05, 2018 11:18:05 Gopan via Digitalmars-d-learn wrote:
> On Tuesday, 5 June 2018 at 10:40:20 UTC, Simen Kjærås wrote:
> > On Tuesday, 5 June 2018 at 09:36:22 UTC, Gopan wrote:
> >> void main()
> >> {
> >>
> >>     immutable n = __ctfe ? 1 : 2;
> >>     int[n] a;
> >>     assert(a.length == n); // fails, wat
> >>
> >> }
> >
> > That's gotta be a bug - that should give a 'variable n cannot
> > be read at compile time' error. The fact that n is immutable
> > shouldn't be enough to use it at compile time. Filed as
> > https://issues.dlang.org/show_bug.cgi?id=18945.
> >
> > --
> >
> >   Simen
>
> Not only immutable.  The behavior is same if you declare n as
> 'const int' also.

It's a bug either way. I suspect that someone made it work as an enhancement
request at some point on the theory that the variable was guaranteed to
always be the same, and they didn't take __ctfe into account. Regardless,
initializing a non-static, local variable shouldn't be triggering CTFE.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list