max() in phobos, and English logic operators

Bill Baxter dnewsgroup at billbaxter.com
Wed Nov 8 15:29:31 PST 2006


Jarrett Billingsley wrote:
> "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))))
>     ...

I'm a Lisp user you insensitive clod!  That looks perfectly natural to 
me!  :-)


> 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).. 

But it's very annoying in C++.

    float x = 17;
    std::max(x,0); // error! which max do you mean - float or int?
    std::max(x,0.0); // error! which max do you mean - float or double?
    std::max(x,0.0f); // ok, both float

Especially annoying for containers parameterized by numeric type. Then 
you don't know what to use for the literal you're comparing against, so 
you've got to use a cast.

    T x = 17;  // could be float,double,real,int etc
    std::max(x, cast(T)0); // ugh.

--bb



More information about the Digitalmars-d-learn mailing list