https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112312

            Bug ID: 112312
           Summary: GCC fails to optimize a C++ algorithm with a function
                    passed in as well as with a lambda
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ville.voutilainen at gmail dot com
  Target Milestone: ---

See 
https://godbolt.org/z/s8dWGzb9r

When passing a (pointer to) function as a predicate argument for a C++ stdlib
algorithm, various people hope that it optimizes as well as passing a lambda
that does exactly the same thing. And with clang, it does. With gcc, the
generated
code is much worse for the function case, even though the function definition
is nearby and visible.

The test code used on godbolt pasted for convenience:

#include <string>
#include <vector>
#include <algorithm>

static bool pred1(const std::string& a, const std::string& b)
{
    return false;
}

auto pred2 = [](const std::string& a, const std::string& b)
{
    return false;
};

bool func1(const std::vector<std::string>& vec, const std::string& needle)
{
    return std::ranges::lower_bound(vec, needle, pred1) != vec.end();
}

bool func2(const std::vector<std::string>& vec, const std::string& needle)
{
    return std::ranges::lower_bound(vec, needle, pred2) != vec.end();
}

Reply via email to