Boost logo

Boost :

Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Christian Holmquist (c.holmquist_at_[hidden])
Date: 2011-09-26 02:30:33


<snip>
..very lengthy complicated discussions
</snip>

Forgive my lack of understanding of the complicated common_type<A, B, C>
scheme, but I would never dare to use boost's implementation of clamp if
taking three types. I don't care much if such an algorithm can be described
in any meaningful way, but if it can, clamp is not an optimal place for such
an exercise.
Sometimes types unfortunately have implicit conversion, and if the compiler
finds them (I guess that what's common_type deduces?),I might end up with a
bogus expression that unfortunately compiles.
Since std::min/max takes one type (for good reasons), I don't see why clamp
all of a sudden should be different.

I would go for something like this...
template<class T>
T const & clamp(T const& x, T const& lo, T const &hi)
{
return std::min(std::max(x, lo), hi);
}

- christian


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