Boost logo

Boost :

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


Vicente J. Botet Escriba wrote:
> Le 26/09/11 13:36, Dave Abrahams a écrit :
> >
> > Note: you can solve that problem by asking the caller to do
> > the conversion ;-)
> >
> I'm not sure this could always be the desired interface for
> the caller. 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.
>
> int v;
> short lo,hi;
> short res= clamp(v, lo,hi);

How does the algorithm select the smaller return type when clamping to a limit, while selecting the larger return type otherwise?

> And in this case I can not convert the 22 bits integer to a 16
> bit because I would lost some essential information.
>
> short res= clamp(short(v), lo,hi); // not the expected
> // behavior

That's the caller's fault, of course.

> That means that I will need to convert to the
>
> short res= short(clamp(v, int(lo),int(hi)));

Right, but all of the information is visible to the caller.

You could also do that like this:

   clamp<int>(v, lo, hi)

For comparison with v, lo and hi must be promoted anyway, so the three-type version doesn't help. Forcing the caller to disambiguate isn't so bad, at least in this case.

_____
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