Boost logo

Boost :

Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-09-26 09:08:09


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.

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

      template <class T, class L, class H, class P>
      T clamp(T const & x, L const & lo, H const & hi, P p);

   Requires: p(x, lo), p(hi, x), T(lo), and T(hi) are well
             formed; lo <= hi

   Returns: T(lo), if p(x, lo); T(hi), if p(hi, x); otherwise x

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

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

   Effects: x = lo, if x < lo; x = hi, if hi < x; otherwise x
             unchanged

   Returns: x

      template <class T, class L, class H, class P>
      T & clamp(T & x, L const & lo, H const & hi, P p);

   Requires: p(x, lo), p(hi, x), x = lo, and x = hi are well
             formed; lo <= hi

   Effects: x = lo, if p(x, lo); x = hi, if p(hi, x); otherwise
             x unchanged

   Returns: x

I'm sure some of that can be expressed more properly, but I think the idea is sufficiently well captured.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP http://www.sig.com

________________________________

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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