max() in phobos?

Bill Baxter dnewsgroup at billbaxter.com
Wed Nov 8 15:52:55 PST 2006


Sean Kelly wrote:
> Bill Baxter wrote:
>> Sean Kelly wrote:
>>> Bill Baxter wrote:
>>>
>>>> Is there a generic 'max' function anywhere in phobos?
>>>> The closest I could find was std.math.fmax().
>>>
>>>
>>> Not that works for all input types, as far as I know.  However, 
>>> something like the code below should work.  This is off the top of my 
>>> head so it may have bugs, but it's a general idea of what such a 
>>> template function may need to do.
>>
>> Great!  How do we get it into std.math?
>>
>> Here's one with the typos fixed and unittests:
> 
> I rewrote the code to work a bit better and added some more tests. 
> However, I think it should probably error when comparing a signed and 
> unsigned integer type.  At the moment, it accepts this and the choice of 
> return type is somewhat arbitrary.  Also, I haven't tested interfaces 
> and am not sure they should be comparable anyway, but I added support 
> for them for the heck of it.  I'd play with it a bit more but I'm tired 
> and bed is calling :-)

Great.  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;

--bb



More information about the Digitalmars-d-learn mailing list