max() in phobos, and English logic operators

David Qualls davidlqualls at yahoo.com
Tue Nov 7 21:35:14 PST 2006


== Quote from Sean Kelly (sean at f4.ca)'s article
> 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.  ...
...
...much code omitted...
...
> template max( T, U )
> {
>      largestOrConvertible!(T,U) max( T t, U u )
>      {
>          return t > u ? t : u;
>      }
> }
> template min( T, U )
> {
>      largestOrConvertible!(T,U) min( T t, U u )
>      {
>          return t < u ? t : u;
>      }
> }

So, is it possible to use templates to define generic binary
operators; like 'and' and 'or', or a unary operator like 'not'?

After looking at the mass of code it takes to implement a simple
generic max() or min() function in D, I'm really starting to pine
for my C preprocessor...

#define max(a,b) ((a)>(b)?(a):(b))

Yeah, I know it breaks if a or b include side effects, but it's
extremely READABLE! (And back on my old soap-box, even FORTRAN and
Basic include English binary logic operators ;-)

DQ



More information about the Digitalmars-d-learn mailing list