challenge: implement the max function
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Sun Jan 21 18:27:16 PST 2007
Frits van Bommel wrote:
> Lionello Lunesu wrote:
>> "Frits van Bommel" <fvbommel at REMwOVExCAPSs.nl> wrote in message
>> news:ep0j0a$1av2$1 at digitaldaemon.com...
>>> Lionello Lunesu wrote:
>>>> "Andrei Alexandrescu (See Website For Email)"
>>>> <SeeWebsiteForEmail at erdani.org> wrote in message
>>>> news:45B3B243.2010103 at erdani.org...
>>>>> Here's a simple challenge: implement the max function. Requirements:
>>>>>
>>>>> a) generic
>>>>> b) efficient
>>>>> c) preserve lvalueness when possible such that one can write e.g.
>>>>>
>>>>> max(arr[0], arr[1]) *= 0.9;
>>>> ...when would that be useful? And, what happens if arr[0]==arr[1] ?
>>> Can't immediately think up an example of usefulness, but what
>>> presumably happens if they're equal is that one of them gets reduced
>>> by 10% (and rounded to the accuracy of typeof(arr[0])).
>>
>> Which one? :)
>
> That would be implementation-dependent.
> If you need it to be a specific one, that's an additional requirement.
> So implement the function yourself ;).
> But perhaps it doesn't matter?
> Or maybe the caller knows it just can't happen?
For conformity purposes, let's just say that in case the two are equal,
the first should be picked.
Andrei
More information about the Digitalmars-d
mailing list