Boost logo

Boost :

Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2011-09-26 13:40:20


Le 26/09/11 15:08, Stewart, Robert a écrit :
> Krzysztof Czainski wrote:
>> I'd like to add my version of clamp declaration. It might be a
>> compromise here:
>>
>> template< class T, class L, class H>
>> T const& clamp( T const& x, L const& lo, H const& hi );
> That's what I had wanted, though the return type cannot be a reference type (making the suggested "in-place" overload useful). The introduction of common_type<T,L,H> complicated matters too much, I think. The idea of this version is simply that the type to clamp is T.
I'm not sure this is always the case. When I want to clip an 22 bit
integer to a 16 bit integer, I expect the result be a 16 bit integer. So
in these cases the result type will be the types of lo and hi. And in
this case I can not convert the 22 bits integer to a 16 bit because I
would lost some essential information.
>
> Off the top of my head:
>
> template<class T, class L, class H>
> T clamp(T const& x, L const& lo, H const& hi);
>
> Requires: x< lo, hi< x, T(lo), and T(hi) are well formed;
> lo<= hi
>
> Returns: T(lo), if x< lo; T(hi), if hi< x; otherwise x

We could add specializations when the 3 parameters have the same type,

template<class T>
       T const& clamp(T const& x, T const& lo, T const& hi);

    Requires: x< lo, hi< x are well formed;
              lo<= hi

    Returns: lo, if x< lo; hi, if hi< x; otherwise x

This will have the advantage to don't requiring the copy of the value.

Best,
Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk