Appending static arrays

Stefan Koch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 17 11:54:31 PDT 2017


On Monday, 17 July 2017 at 18:38:16 UTC, Nordlöw wrote:
> On Monday, 17 July 2017 at 17:46:42 UTC, ag0aep6g wrote:
>>     int[n + m] result = a ~ b;
>
> Further, the expression `a ~ b` here will allocate and create a 
> copy on the GC-heap before writing `result`.
>
> Maybe LDC optimizes away this now or in the future but DMD 
> cannot.
>
> Yeah I know, kind of dumb but that's how it is currently.

we have special code in the compiler to optimize a ~ b ~ c.
So all you need to do make it so
auto cat(T[]...)(T args)
{
     T[] result;
     mixin(() {
       string mix  = `result = `;
       foreach(i;args.length)
       {
         mix ~= `args[` ~ itos(i) ~ `] ~`;
       }
       mix[$-1] = ';';
       return mix;
     }());

     return result;
}

that should do it :)


More information about the Digitalmars-d-learn mailing list