max() in phobos, and English logic operators
kb3ctd2 at yahoo.com
Tue Nov 7 22:32:09 PST 2006
"David Qualls" <davidlqualls at yahoo.com> wrote in message
news:eirqai$he$1 at digitaldaemon.com...
> So, is it possible to use templates to define generic binary
> operators; like 'and' and 'or', or a unary operator like 'not'?
Not like the way you'd use the iso646 ones. It'd be really unnatural:
if(not(and(a, or(b, c))))
> 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 ;-)
Well, if you make the max function a bit less generic, restricting it to
just one input type, it becomes
T max(T)(T a, T b)
return (a > b) ? a : b;
Which is pretty readable to me, and probably covers most of the use cases.
Most of that conversion stuff in Sean's implementation is probably type
trait stuff which would best belong in std.traits (when it comes out)..
More information about the Digitalmars-d-learn