challenge: implement the max function

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Sun Jan 21 18:19:14 PST 2007


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] ?

It's useful to implement specs like "amortize the largest of a and b by 
0.9". If they are equal, an arbitrary one of them will be amortized.

More to the point, preserving lvalueness is important for functions with 
different semantics than max; max is a simple enough problem to work on 
the types aspect without getting distracted by its logic.


Andrei



More information about the Digitalmars-d mailing list