Is enum static?
John Colvin
john.loughran.colvin at gmail.com
Tue Aug 20 12:33:23 PDT 2013
On Tuesday, 20 August 2013 at 19:25:56 UTC, Jonathan M Davis
wrote:
> On Tuesday, August 20, 2013 20:38:52 John Colvin wrote:
>> On Tuesday, 20 August 2013 at 17:02:07 UTC, Jonathan M Davis
>>
>> wrote:
>> > On Tuesday, August 20, 2013 12:54:29 John Colvin wrote:
>> >> is there an allocation in this?
>> >>
>> >> enum vals=[1, 2, 3, 0];
>> >>
>> >> int[4] a;
>> >> a[] = vals[];
>> >
>> > Since, you're asking it to copy the elements of a dynamic
>> > array
>> > to a static
>> > one, I would fully expect it to result in an allocation,
>> > though
>> > a smart
>> > compiler might optimize it out. I wouldn't expect dmd to do
>> > that though.
>> >
>> > - Jonathan M Davis
>>
>> So you're saying it will allocate a new dynamic array,
>> initialise
>> it with 1,2,3,0 and then copy the elements from that new array
>> to
>> the static one? That's not good...
>
> Well, that's what you told it to do semantically. The compiler
> could
> theoretically optimize it (and hopefully will eventually), but
> it would be an
> optimization. At this point, if you initialize the static array
> with an array
> literal, then it will avoid the allocation (though it didn't
> used to), but
> AFAIK, it'll still allocate with your example.
>
> - Jonathan M Davis
I presume there's a good reason why we don't have:
enum a = [1,2,3,4];
assert assert(is(typeof(a) == int[4]));
this works after all:
enum int[4] a = [1,2,3,4];
assert assert(is(typeof(a) == int[4]));
More information about the Digitalmars-d-learn
mailing list