MIN MAX problem

Daniel Keep daniel.keep.lists at gmail.com
Tue Mar 13 19:56:51 PDT 2007


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

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list