Chain two different struct specialization

Andrea Fontana nospam at example.com
Fri Mar 1 08:49:18 PST 2013


On Friday, 1 March 2013 at 16:38:39 UTC, Era Scarecrow wrote:
> 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...

Maybe if i have MyStruct!Weights1 arr[];  MyStruct!Weights2 
arr2[];
instead of trying to merge them, and call:
myfunc(singlebigarray);

I should write a template function with a variadic number of 
params:
myfunc(arr1, arr2, arr3 etc...);

Or simply come back to basic ineritance or previous solution, was 
just a challenge to find a compile time way to pre-compute all 
consts factors...






More information about the Digitalmars-d-learn mailing list