MIN MAX problem
freeagle
dalibor.free at gmail.com
Tue Mar 13 03:35:21 PDT 2007
Daniel Keep wrote:
> freeagle wrote:
>> Walter Bright wrote:
>>> torhu wrote:
>>>> You can use typeof to get the common type, like this:
>>>>
>>>> typeof(T + U) max(T, U)(T arg1, U arg2)
>>>> {
>>>> return arg1 > arg2 ? arg1 : arg2;
>>>> }
>>> The + isn't always defined, so you can use:
>>>
>>> typeof(true ? cast(T)0 : cast(U)0) max(T, U)(T x1, U x2)
>>> {
>>> return x1 > x2 ? x1 : x2;
>>> }
>> could you please explain what this means? coz i'd say it always returns T
>>
>> typeof(true ? cast(T)0 : cast(U)0)
>>
>> And, i think this wont work for classes that overload the >,< etc
>> operator, as you can't do cast(class)0, can you?
>
> In the archives somewhere, there is a long thread discussing an
> implementation of min (or max; can't remember). From what I do
> remember, it basically boiled down to "good grief this is actually
> really hard once you involve types!".
>
> Might be worth searching for it and having a read. In fact, because I'm
> feeling so helpful this morning:
>
> http://www.digitalmars.com/d/archives/digitalmars/D/challenge_implement_the_max_function_47026.html
>
> Hope this helps
>
> -- Daniel
>
aha, sorry, i wasnt really followig the development and talk about D for
2 or 3 months, so missed it
More information about the Digitalmars-d
mailing list