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

            Bug ID: 107850
           Summary: std::erase_if (map) forces predicate to takes a const
                    value_type
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chgros at synopsys dot com
  Target Milestone: ---

g++11 will compile this program, but g++ 12.1.0 will not, because it complains
that the predicate cannot take a const.
The standard definition of erase_if (reproduced here for comparison purposes)
indicates that g++11 is right in this case; this is a regression.

#include <map>
#include <string>

using namespace std;

template<typename Pred>
int erase_if_by_std(map<string, string> &c, Pred pred)
{
    auto original_size = c.size();
    for (auto i = c.begin(), last = c.end(); i != last; ) {
        if (pred(*i)) {
            i = c.erase(i);
        } else {
            ++i;
        }
    }
    return original_size - c.size();
}

int main(int argc, char const * const *argv)
{
    auto pred = [&](pair<string const, string> &p) {
        if(p.first.size() == 2) {
            return true;
        } else {
            p.second.resize(3);
            return false;
        }
    };
    map<string, string> m;
    erase_if_by_std(m, pred);
    std::erase_if(m, pred);
}

Reply via email to