max() in phobos, and English logic operators

Sean Kelly sean at
Wed Nov 8 05:48:58 PST 2006

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

They're in the C++ standard library, so I don't see why 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!

There has been a lot of work done in the C++ community to produce a 
template function that is as functional as this simple macro.  In fact, 
I think Andrei actually wrote a series of articles on it.  I think we 
can do better in D because of the improved compile-time type 
information, but it's difficult to match the simplicity of macro code 


More information about the Digitalmars-d-learn mailing list