
On 26 September 2011 10:12, Olaf van der Spek <ml@vdspek.org> wrote:
On Mon, Sep 26, 2011 at 5:04 PM, Christian Holmquist <c.holmquist@gmail.com> wrote:
Is this really the fault of clamp() though?
No, but clamp can avoid exposing this inherited problem, just as std::min/std::max does.
min and max have two 'equivalent' parameters. clamp doesn't, it has two limits and another argument. The return type is clear, it's the type of the first argument, that's not the case for min and max.
Why is the result type the first argument, since any of the three may be returned? Why is that clear for clamp? clamp(T x, Low lo, High hi) if (x < lo) return lo; ^---------- doesn't return T. else if (!(x < hi)) return hi; ^---------- doesn't return T. else return x;