Help with Template Code

Max Samukha samukha at voliacable.com
Sun Apr 1 01:19:42 PDT 2007


On Sat, 31 Mar 2007 20:55:54 -0400, "Jarrett Billingsley"
<kb3ctd2 at yahoo.com> wrote:

>"Frits van Bommel" <fvbommel at REMwOVExCAPSs.nl> wrote in message 
>news:eum0c9$2b75$1 at digitalmars.com...
>>
>> Come on, we can do a bit better than that!
>> ---
>> template StructCtor()
>> {
>>     static typeof(*this) opCall(typeof(typeof(*this).tupleof) args)
>>     {
>>         typeof(*this) t;
>>
>>         foreach(i, arg; args)
>>             t.tupleof[i] = arg;
>>
>>         return t;
>>     }
>> }
>>
>> struct S
>> {
>>     int x;
>>     float y;
>>     char[] z;
>>
>>     mixin StructCtor;
>> }
>
>Ahh, I was hoping that was possible! 
>
Note that the mixin ctor is slower then manually coded one because of
the loop. To improve performance, you could skip the struct
initialization: 

template StructCtor()
{
     static typeof(*this) opCall(typeof(typeof(*this).tupleof) args)
     {
         typeof(*this) t = void;

         foreach(i, arg; args)
             t.tupleof[i] = arg;

         return t;
     }
 }


More information about the Digitalmars-d-learn mailing list