2

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.

NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • Is there a "fundamental" reason why it's remotely necessary? Use a [function adapter](https://stackoverflow.com/a/67622901/2069064). – Barry Jul 19 '21 at 16:10
  • A range doesn't necessarily have a container behind it. – Pete Becker Jul 19 '21 at 16:15
  • @Barry just syntax suggar, for me it is much nicer to write &MyStruct::member_ , but that is a personal style thing... – NoSenseEtAl Jul 19 '21 at 16:20
  • 1
    @NoSenseEtAl `erase_if(v, proj(&MyStruct::member_, pred))`? – Barry Jul 19 '21 at 16:21
  • @Barry yes, but that is not std:: I know this is a stupid reason, but often it is much easier to sell things to other people when something is in std... but you are right, if one can use Boost then HOF is nice option. – NoSenseEtAl Jul 19 '21 at 16:23
  • `std::erase_if( vec, pred )` -- isn't `pred` a projection? Or is it that you want `std::erase_if(vec, pred(proj) )`? – Yakk - Adam Nevraumont Jul 20 '21 at 13:31
  • @Yakk-AdamNevraumont I meant something like: erase(container, 47, &Person::age) -> deletes all persons from container if their age is 47 – NoSenseEtAl Jul 20 '21 at 14:13

1 Answers1

3

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.

Caleth
  • 52,200
  • 2
  • 44
  • 75