challenge: implement the max function

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Mon Jan 22 02:47:36 PST 2007


Don Clugston kirjoitti:
> Lionello Lunesu wrote:
>> donth ave wrote:
>>> Xinok Wrote:
>>>
>>>> T[0]+T[1]
>>> To have addition is an additional(sic!) requirement not present in the
>>> challenge.
>>
>> Would typeof(T[0]>T[1]?T[0]:T[1]) work? I'd have to test it, I guess :)
> 
> No, it doesn't work. But this does:
> (starting to fail the simplicity requirement though...)

Squeezing a bit might help f) a minor bit more:

template maxtype(T...){
    static if(T.length == 0) static assert(false);
    static if(T.length == 1) alias typeof(T[0]) maxtype;
    static if(T.length == 2 && is( typeof( (T[0] x, T[1] y){ return y <
x ? y : x;}) Q == return)) {
        alias Q maxtype;
    }
    static if(T.length > 2) {
        alias maxtype!(maxtype!(T[0..1]), T[2..$]) maxtype;
    }
}

maxtype!(T) max(T...)(T arg){
    static if(arg.length == 1) return arg[0];
    static if(arg.length > 1) return arg[0] >= arg[1] ? arg[0] :
max(arg[1..$]);
}


Doesn't it work as an lvalue for classes now?



More information about the Digitalmars-d mailing list