Why does this work?

Jonathan M Davis jmdavisProg at gmx.com
Thu Feb 17 21:22:13 PST 2011


On Thursday 17 February 2011 21:06:26 Jason House wrote:
> Vladimir Panteleev Wrote:
> > int foo;
> > enum bar = foo+2;
> > 
> > void main()
> > {
> > 
> > 	foo = 7;
> > 	assert(bar == 9);
> > 
> > }
> 
> I would have expected bar to equal 2 since foo would be default initialized
> to 2. I'm going to guess that the there's some kind of optimization that
> only assigns to foo once and isn't noticing that bar depends on it. You
> should probably post that in bugzilla.

I believe that the bug here is that bar is allowed to use foo in its 
initialization. foo isn't immutable or an enum, so that shouldn't work. bar 
probably gets replaced with foo + 2, which would be perfectly legitimate were 
foo actually immutable. And the foo + 2 probably doesn't get optimized out like 
it should be, because foo isn't actually immutable like it would have to be (per 
the spec) to be used in bar's initialization.

This is _definitely_ a bug.

- Jonathan M Davis


More information about the Digitalmars-d mailing list