This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [libstdc++] Add C++17clamp
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: Ed Smith-Rowland <3dw4rd at verizon dot net>
- Cc: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 21 Jul 2016 19:24:19 +0100
- Subject: Re: [libstdc++] Add C++17clamp
- Authentication-results: sourceware.org; auth=none
- References: <57882528.8010501@verizon.net> <20160715073410.GO12493@redhat.com> <57891E9F.90701@verizon.net>
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.