Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why std::erase and std::erase_if don't support projection?

C++ ranges are nice, but AFAIK they still "suffer" from the fact that they do not know to modify containers, e.g. if you use ranges::remove you still need to do container.erase(...

Now there are algorithms that do know how to erase from containers (std::erase, std::erase_if) but unlike ranges they do not support projection.

My question if this is just because (AFAIK) that functionality was proposed separately from ranges(+ lack of time/lack of proposals), or is there fundamental reason why this functionality is not available.

like image 478
NoSenseEtAl Avatar asked Sep 12 '25 05:09

NoSenseEtAl


1 Answers

std::erase and std::erase_if are not algorithms applicable to any container. They are an overload set of functions that do "the same thing" to many containers.

The associative containers don't have std::erase, because it would either be inconsistent with their member erase, or it would be inconsistent with the sequence container erase.

I don't think there is a fundamental incompatibility with having a projection argument in erase_if, nor in erase where it is present, but they were defined in terms of the existing std::remove, std::remove_if and member erases and removes, which lack projections.

like image 112
Caleth Avatar answered Sep 14 '25 20:09

Caleth