This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [libstdc++] Add C++17clamp

On 22/07/16 19:38 +0200, Daniel Krügler wrote:
2016-07-22 9:55 GMT+02:00 Jonathan Wakely <>:
On 22/07/16 08:51 +0100, Jonathan Wakely wrote:

On 21/07/16 19:38 -0400, NightStrike wrote:

On Thu, Jul 14, 2016 at 7:50 PM, Ed Smith-Rowland <>

Here is an implementation of P0025
An algorithm to "clamp" a value between a pair of boundary values.

Testing is almost finished - looks good so far.

OK if testing passes?

I didn't see a feature test in any of the SD-6 papers or P0025.

This is not an efficient implementation.  See here:

Which I derived from this SO answer (which is sadly not the accepted
answer at this time):

I suggest using the very efficient method that requires a temporary.

That isn't a valid implementation of std::clamp, since it performs a
copy. The template argument might not even be copyable.

We could possibly dispatch to such an implementation for arithmetic
types, but we wouldn't want to do it for all copyable types. There's
no way you can know whether making that local copy is expensive for an
arbitrary type, and making a copy isn't allowed anyway.

But given that clamp is required not even to *return* copies of any of
it's arguments, it doesn't seem to be possible to do that for
arithmetic types either, unless I'm misunderstanding something very
fundamentally here. Or are you interpolating that the same performance
will result out of (ignoring constexpr) the following transformation

const float& clamp(const float& x, const float& min, const max) {
 const float& t = x < min ? min : x;
 return t > max ? max : t;


No, I was just overlooking the fact that of course we can't make a
copy if we have to return a reference to one of the arguments.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]