From: Giuseppe D'Angelo Date: Sun, 19 Jan 2025 15:30:20 +0000 (+0100) Subject: libstdc++: perfectly forward std::ranges::clamp arguments X-Git-Tag: releases/gcc-14.3.0~548 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d121d1e1dd874731a648086772e6c40e34fa5de9;p=thirdparty%2Fgcc.git libstdc++: perfectly forward std::ranges::clamp arguments As reported in PR118185, std::ranges::clamp does not correctly forward the projected value to the comparator. Add the missing forward. libstdc++-v3/ChangeLog: PR libstdc++/118185 PR libstdc++/100249 * include/bits/ranges_algo.h (__clamp_fn): Correctly forward the projected value to the comparator. * testsuite/25_algorithms/clamp/118185.cc: New test. Signed-off-by: Giuseppe D'Angelo Reviewed-by: Patrick Palka Reviewed-by: Jonathan Wakely (cherry picked from commit b342614139c0a981b369176980663941b9c27f39) --- diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index a29d387dc711..0386e7aafa6d 100644 --- a/libstdc++-v3/include/bits/ranges_algo.h +++ b/libstdc++-v3/include/bits/ranges_algo.h @@ -2945,9 +2945,13 @@ namespace ranges std::__invoke(__proj, __hi), std::__invoke(__proj, __lo)))); auto&& __proj_val = std::__invoke(__proj, __val); - if (std::__invoke(__comp, __proj_val, std::__invoke(__proj, __lo))) + if (std::__invoke(__comp, + std::forward(__proj_val), + std::__invoke(__proj, __lo))) return __lo; - else if (std::__invoke(__comp, std::__invoke(__proj, __hi), __proj_val)) + else if (std::__invoke(__comp, + std::__invoke(__proj, __hi), + std::forward(__proj_val))) return __hi; else return __val; diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/118185.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/118185.cc new file mode 100644 index 000000000000..e9cf896d3556 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/clamp/118185.cc @@ -0,0 +1,41 @@ +// { dg-do compile { target c++20 } } + +#include +#include + +struct Comp +{ + constexpr bool operator()(const int&& x, const int&& y) { return x < y; } +}; + +struct Proj +{ + constexpr const int&& operator()(const int& x) const { return std::move(x); } +}; + +static_assert(std::indirect_strict_weak_order>); + +static_assert(std::ranges::clamp(+1, 0, 2, Comp{}, Proj{}) == 1); +static_assert(std::ranges::clamp(-1, 0, 2, Comp{}, Proj{}) == 0); +static_assert(std::ranges::clamp(10, 0, 2, Comp{}, Proj{}) == 2); + + +// Testcase from PR118185 + +struct Comp2 +{ + constexpr bool operator()(const int&& x, const int&& y) const { return x < y; } + constexpr bool operator()(const int&& x, int& y) const { return x < y; } + constexpr bool operator()(int& x, const int&& y) const { return x < y; } + constexpr bool operator()(int& x, int& y) const { return x < y; } + constexpr bool operator()(std::same_as auto && x, std::same_as auto && y) const + { + return x < y; + } +}; + +static_assert(std::indirect_strict_weak_order>); + +static_assert(std::ranges::clamp(+1, 0, 2, Comp2{}, Proj{}) == 1); +static_assert(std::ranges::clamp(-1, 0, 2, Comp2{}, Proj{}) == 0); +static_assert(std::ranges::clamp(10, 0, 2, Comp2{}, Proj{}) == 2);