[Issue 1845] Variant and VariantN cannot handle structs of arbitrary size

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 5 14:40:22 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=1845





------- Comment #2 from dsimcha at yahoo.com  2009-04-05 16:40 -------
I'd say the best compromise between a well-encapsulated solution and a solution
that avoids hidden allocations is to have something like a bigAssign(T) method
in addition to the regular opAssign.  It would work something like this:

struct Huge {
    real a, b, c, d, e, f, g;
}

void main() {
    Huge huge;
    Variant v;
    v = huge;  // Compile time error.
    v.bigAssign(huge);  // Works, allocates on heap transparently.
    v.bigAssign(5);  // Works, equivalent to v = 5 or v.opAssign(5).
}

Heck, you could even make this a policy.  Let's say you have two functions: 
neverAllocate() and allocateAsNeeded().  You could make opAssign() an alias to
one of these instead of a funciton.  The VariantN template could take an alias
parameter for opAssign that defaults to one of the two, but can be overridden
on instantiation.  Not sure which the default should be, but that's a minor
detail.


-- 



More information about the Digitalmars-d-bugs mailing list