C++17 cannot beat D surely

Yuxuan Shui via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 6 17:28:23 PDT 2017


On Sunday, 4 June 2017 at 04:39:21 UTC, Adam D. Ruppe wrote:
> On Sunday, 4 June 2017 at 04:34:44 UTC, Mike Parker wrote:
>> I would not have expected enum b = sort(a) to trigger an 
>> allocation. auto b, yes, of course (and the disassembly from 
>> that is not much different). So I'd love to see a blog post 
>> explaining it.
>
> I don't think I can do a full-on blog post, but I can answer it 
> in a couple sentences: `enum` is treated by the compiler just 
> like literals. Array literals allocate at each usage point, 
> therefore enum arrays allocate at each usage point. (*each* 
> usage point)

I think the allocation is because sort returns a SortedRange, not 
an array.

So even this allocates:

     enum b = a.sort;
     writeln(b[1]);

OTOH, this doesn't allocate:

     enum b = a.sort.array;
     writeln(b[1]);


More information about the Digitalmars-d mailing list