MIN MAX problem

freeagle dalibor.free at gmail.com
Mon Mar 12 11:31:00 PDT 2007


Hey all,

from what I know, there is (currently?) no way of implementing min/max 
template functions that could dynamically change return type depending 
on the result of comparison. I think that this problem can be "solved" 
easily. When used in C++, there is no problem with the return type as 
min/max are coded as macros. But there is always probability that the 
result of the min/max will be cast into appropriate type, because 
variables cant be both float and int at the same time, for example.
That means, when you write in C++

float f = 1.0f, result;
int a = 2;
result = MAX(f, a);

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.

Just a thought

freeagle



More information about the Digitalmars-d mailing list