structs, templates, alias

Robert Clipsham robert at octarineparrot.com
Sun Apr 25 12:50:28 PDT 2010


On 25/04/10 20:32, Ellery Newcomer wrote:
> Hmm. Either I'm not understanding you or I didn't explain something
> clearly.
>
> something like this
>
> struct Rec2{
> ushort index;
> ushort nparams;
> ushort options;
> ushort[] params; // this has nparams elements
> }
>
> Rec2 rec2 = {index:1, nparams:2, options:~0, params:[4,5]};
>
> would translate to something like
>
> x"01 00 02 00 FF FF 04 00 05 00"
>
> only the elements part of params gets written out, the length part doesn't.

Ah, I didn't know this. In which case you should be able to store 
nparams for use later when the array is encountered, then parse as 
appropriate?
----
static nparams = 0;
foreach( m; MyStruct.tupleof )
{
   static if( is( typeof(m) x : x[] ) )
   {
     parseParams(nparams);
   }
   else
   {
     static if( MyStruct.tupleof[i].stringof[$-7 $] == "nparams" )
       nparams = m;
   }
}
----


More information about the Digitalmars-d-learn mailing list