Chain two different struct specialization
Era Scarecrow
rtcvb32 at yahoo.com
Fri Mar 1 08:38:35 PST 2013
On Friday, 1 March 2013 at 14:32:12 UTC, Andrea Fontana wrote:
>> struct MyStruct(WEIGHTS)
>> {
>> string ...
>> string ...
>>
>> alias WEIGHTS weights;
>> }
>>
>> enum FirstWeights : double
>> {
>>
>> }
>
> enum SecondWeights : double
> {
> double foo = 0.3,
> double bar = 0.4
> }
>
> so:
> auto s1 = MyStruct!FirstWeights ...
> auto s2 = MyStruct!SecondWeights ...
With templates there's a few different ways you can consider
handling it; I'm not recommending any of them but. Keeping it
simple, clean & easy to read is probably the best solution;
Although most of these won't help with any algorithms that need
them to be identical (unless cast or they are not templates).
1) If structs are the same size: You can forcibly cast it so one
will work with the other, however enums (or anything related
that's static or not stored with the struct) doesn't transfer
over and is lost.
I've tried this in my experimental polymorphic struct; Where the
methods differed slightly but data the structure is guaranteed to
be identical.
2) Conversion/Accessing differences: You can include a flag
specifying it's of a certain type or qualification, this can let
them interact if they are basically the same thing with something
minor under the hood different but that doesn't matter (or will
get in the way); Although unqual (or related in std.traits) might
be a better option for it, not sure myself.
struct MyStruct(WEIGHTS) {
enum isMyStruct = true;
}
void test(T1, T2)(T1 lhs, T2 rhs)
if (hasMember!(T1, "isMyStruct") && hasMember!(T2,
"isMyStruct")) {}
3) non-Template: As mentioned you can avoid templates &
different enums and instead use variables as appropriate.
I'm sure there's other ideas but I'm drawing a blank right now...
More information about the Digitalmars-d-learn
mailing list