[libstdc++] Add C++17clamp

Jonathan Wakely jwakely@redhat.com
Thu Jul 21 18:24:00 GMT 2016


On 15/07/16 13:34 -0400, Ed Smith-Rowland wrote:
>
>>OK for trunk, thanks.
>>
>>>I didn't see a feature test in any of the SD-6 papers or P0025.
>>
>>p0096r3 proposes __cpp_lib_clamp = 201603.
>I added the feature macro and committed the attached as 238383.
>
>Thanks,
>
>Ed
>

>2016-07-15  Edward Smith-Rowland  <3dw4rd@verizon.net>
>
>	Implement C++17 P0025 clamp.
>	* include/bits/algorithmfwd.h: Declare clamp overloads.
>	* include/bits/stl_algo.h: Implement clamp.  Feature __cpp_lib_clamp.
>	* testsuite/25_algorithms/clamp/1.cc: New test.
>	* testsuite/25_algorithms/clamp/2.cc: New test.
>	* testsuite/25_algorithms/clamp/constexpr.cc: New test.
>	* testsuite/25_algorithms/clamp/requirements/explicit_instantiation/
>	1.cc: New test.
>	* testsuite/25_algorithms/clamp/requirements/explicit_instantiation/
>	pod.cc: New test.
>

>+  const int xc = std::clamp(1, 2, 4, std::greater<int>());
>+  const int yc = std::clamp(3, 2, 4, std::greater<int>());
>+  const int zc = std::clamp(5, 2, 4, std::greater<int>());

These all violate the precondition that !comp(hi, lo)
i.e. !greater<int>(1, 0)

The arguments need to be re-arranged, and then these are wrong:

>+  VERIFY( xc == 4 );
>+  VERIFY( yc == 2 );
>+  VERIFY( zc == 2 );


>+static_assert(std::clamp(2, 0, 1) == 1, "");
>+static_assert(std::clamp(2, 0, 1, std::greater<int>()) == 0, "");

Same here. If the arguments are clamp(2, 1, 0, greater<int>()) then it
returns 1.



More information about the Gcc-patches mailing list