Boost logo

Boost :

Subject: [boost] [Review:Algorithms] Order of args to clamp
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2011-09-23 12:21:23


Dear All,

Some quick comments about the proposed clamp() function:

   template<typename V>
   V clamp ( V val, V lo, V hi );

I have a clamp function that orders the args low - middle - high, which
seems like a more natural ordering to me. Is there some rationale or
precedent for this middle - low - high ordering? I think the confusion
matters in this case because type checking won't help to detect
mistakes, and in the most obvious applications (e.g. sanitising input)
it might be hard to spot an error. (In contrast,
max(low,min(mid,high)) is unambiguous and not much more typing...)

I note that this function takes its args by value, while std::min & max
take const references. What is the rationale for this?

Like min & max, clamp has a single type template parameter; what are
the implications of this? For example, if I pass a mixture of
std::string and const char* arguments, what will happen? Ideally, (a)
all combinations would compile, and (b) conversions from const char* to
std::string will only happen when necessary, not unconditionally for
all of the arguments. (Maybe that is asking too much, though.)

Regards, Phil.


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