On 07/03/15 12:52, Laeeth Isharc via Digitalmars-d-learn wrote: > I have an array of structs eg > > struct PriceBar > { > DateTime date; > double open; > double high; > double low; > double close; > } > > (which fields are present in this particular struct will depend on template > arguments). > > what is the best way to turn these at compile time into a struct of arrays? eg > > struct PriceBars > { > DateTime[] dates; > double[] opens; > double[] highs; > double[] lows; > double[] closes; > } > > I can use FieldTypeTuple and FieldNameTuple, but I am a bit lost as to how > without static foreach to loop through these in order to generate a mixin to > declare the new type. I can turn it into a string, but what is the better > option?
The simplest solution is something like: template SOA(Struct, size_t LENGTH) { struct SOA { enum MEMBERNAME(size_t N) = __traits(identifier, Struct.tupleof[N]); static __gentypes() { string ret; foreach (I, TYPE; typeof(Struct.tupleof)) ret ~= "align(16) typeof(Struct.tupleof["~I.stringof~"])["~LENGTH.stringof~"] " ~ MEMBERNAME!I ~ ";"; return ret; } mixin(__gentypes()); } } alias PriceBars = SOA!(PriceBar, 8); which you'll have to adjust to your requirements (eg drop the '"~LENGTH.stringof~"' part to get a struct-of-slices, which is what your example above shows). artur