max() in phobos, and English logic operators

Sean Kelly sean at f4.ca
Thu Nov 9 09:23:01 PST 2006


David Qualls wrote:
> == Quote from Bill Baxter (dnewsgroup at billbaxter.com)'s article
> 
>> But my point was just that the extra information is not
> necessary for
>> the macro version.  The intended meaning of max(x,0) is clear
> enough to
>> the reader, so the compiler should be able to handle it without
>> complaining, too.
>> --bb
> 
> EXACTLY!  I also think you (mostly) nailed it with
> 
>> About signed types, I think
>>     auto c = max(a,b)
>> should pick the same type for c that the compiler would pick for
>>     auto c = a+b;
> 
> except I don't think max(10U , -1) should return -1 cast to an
> unsigned!

The addition promotion rules for integers are essentially as follows:

int + uint = uint
uint + long = long
(anything smaller than int) + (anything smaller than int) = int

It would be easy enough to simply use typeof(T.init + U.init) for 
concrete types and throw out a bunch of static ifs, but I'm not sure if 
integer promotion is desired here.  Suggestions?


Sean



More information about the Digitalmars-d-learn mailing list