August 29, 2007
Often an opCmp implementation will end up just being a comparison based on a single member of elementary type.

Is there any function in Phobos for doing an opCmp style comparison of the basic types?  Something like

int compare(S,T)(S a, T b) {
    if (a==b) return 0;
    return (a<b)? -1 : 1;
}

It's simple but comes up enough that it seems like it would be useful to have in the library.  Kinda like min/max.... which aren't in Phobos either.

-bb
August 29, 2007
Bill Baxter wrote:
> Often an opCmp implementation will end up just being a comparison based on a single member of elementary type.
> 
> Is there any function in Phobos for doing an opCmp style comparison of the basic types?  Something like
> 
> int compare(S,T)(S a, T b) {
>     if (a==b) return 0;
>     return (a<b)? -1 : 1;
> }
> 
> It's simple but comes up enough that it seems like it would be useful to have in the library.  Kinda like min/max.... which aren't in Phobos either.
> 
> -bb
You can use typeinfo-based comparisons.
return typeid(typeof(a)).compare(&a, &b); // ... I think
I'm not sure if this actually works, but it should. Only good for
comparing the same type though - otherwise we're stuck with what you
proposed. And I agree, things like your code should be in the library.
 --downs