Array of templated classes or structs

TheFlyingFiddle via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Oct 24 12:00:56 PDT 2015


On Saturday, 24 October 2015 at 18:40:02 UTC, TheFlyingFiddle 
wrote:
> To complete TemplateStruct simply forward the remaing members 
> of the
> variant. Or use something like proxy!T in std.typecons. Or use 
> an alias this v.
> (I don't really recommend alias this it has all kinds of 
> problems)

One thing about variant is that if the struct you are trying to 
insert is larger then (void delegate()).sizeof it will allocate 
the wrapped type on the gc heap. This might be detrimental to 
performance. So to help with this you could add an extra element 
on the TemplateStruct to sort of handle this.

struct TemplateStruct(alias template_, size_t size = (void 
delegate).sizeof)
{
     VariantN!(size) v;
     //Rest is the same.
}

Pick a good size for the template you want to make arrays of and 
it will lessen the stress on the gc heap.

For example:
struct vec4(T)
{
     T[4] data;
     //stuff
}

alias Vector4 = TemplateStruct!(template_, vec4!(double).sizeof);
Vector4[] array;

Additionaly you might want to look into the 
(http://forum.dlang.org/thread/jiucsrcvkfdzwinqpzxg@forum.dlang.org) if your interested in some cool stuff that can be done to call methods on such variant structs.



More information about the Digitalmars-d-learn mailing list