MIN MAX problem
freeagle
dalibor.free at gmail.com
Mon Mar 12 14:03:37 PDT 2007
torhu wrote:
> freeagle wrote:
>> the result is cast to float when assignment to the variable is being
>> done. This means you always know of what type you want the result to be.
>> I say we could move the casting into the function itself, like this:
>>
>> T max(T, U)(T arg1, U arg2)
>> {
>> return cast(T) (arg1 > arg2 ? arg1 : arg2);
>> }
>>
>> and make it a standard that the type of the first argument will always
>> be the returning type of the min/max function.
>
> 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;
> }
havent seen in the docs something like type adding ?! anyway, my guess
is that the typeof(T + U) expresion always return the the same type for
each couple of types. so float + int would always return float? what if
you want it to return int?
More information about the Digitalmars-d
mailing list