November 14, 2006 Re: max() in phobos? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote:
> 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. This is off the top of my head so it may have bugs, but it's a general idea of what such a template function may need to do.
>
> /**
> * Returns the largest of the two supplied types, or the first type if
> * the sizes are identical. If either type is an object then both must
> * be objects of either the same type or where one is a base class of
> * the other. Interfaces are not supported.
> */
> template largestOrConvertible( T, U )
> {
> static if( is( T : Object ) || is( U : Object ) )
> {
> static assert( is( T : Object ) && is( U : Object ),
> "Types incompatible." );
>
> static if( T : U )
> alias T largestOrConvertible;
> else static if( U : T )
> alias U largestOrConvertible;
> else static assert( false, "Object types must be related." );
> }
> else static if( is( T : Interface ) || is( U : Interface ) )
> static assert( false, "Interface types not yet supported." );
> else static if( T.sizeof < U.sizeof )
> alias U largestOf; // concrete type, U larger
> else alias T largestOf; // concrete type, T larger or equal
> }
>
> 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;
> }
> }
We could do all of this code, or we could just have type inference for return values:
auto max (T, U) (T t, U u)
{
return t > u ? t : u;
}
Problem solved!
|
November 14, 2006 Re: max() in phobos, and English logic operators | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | == Quote from John Reimer (terminal.node@gmail.com)'s article > On Wed, 08 Nov 2006 05:48:58 -0800, Sean Kelly <sean@f4.ca> wrote: > > David Qualls wrote: > >> 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... > Blech! I do not find C/C++ macros readable! ... > Oh... that feels better. It seems I periodically have to bash > the CPP... > at least once a year, at least. :) > -JJR Well, since I was the one that brought it up, I feel I deserve payment for the positive psycho-therapy you just experienced! ;-) David |
November 14, 2006 Re: max() in phobos? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reiner Pope | "Reiner Pope" <reiner.pope@REMOVE.THIS.gmail.com> wrote in message news:ejbpi0$194t$1@digitaldaemon.com... > We could do all of this code, or we could just have type inference for return values: > > auto max (T, U) (T t, U u) > { > return t > u ? t : u; > } > > Problem solved! That's true. D already does return type deduction for delegate literals.. |
November 14, 2006 Re: max() in phobos, and English logic operators | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Qualls | LOL! Well, I hope you don't charge too much! :D -JJR |
Copyright © 1999-2021 by the D Language Foundation