[Bug libstdc++/112349] ranges::max makes unnecessary copies
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Nov 2 18:02:29 GMT 2023
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112349
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Last reconfirmed| |2023-11-02
Status|UNCONFIRMED |NEW
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Ted Lyngmo from comment #0)
> auto& __tmp = *__first;
That won't compile for a move_iterator or a proxy reference. I think auto&&
would be OK.
But we'd need to restore the value category:
auto __result = *__first;
while (++__first != __last)
{
auto&& __tmp = *__first;
if (std::__invoke(__comp,
std::__invoke(__proj, __result),
std::__invoke(__proj, __tmp)))
__result = std::forward<decltype(*__first)>(__tmp);
}
return __result;
> ...or just supplying `*__first` to `std::__invoke` it doesn't do copies in
> OP's example but it may have other consequences. Worth looking in to anyway.
I think that would be OK too.
auto __result = *__first;
while (++__first != __last)
{
if (std::__invoke(__comp,
std::__invoke(__proj, __result),
std::__invoke(__proj, *__first)))
__result = *__first;
}
return __result;
More information about the Gcc-bugs
mailing list