I know algorithms (e.g. sort
) in ranges support projection, but it seems to me that there is no way to get that functionality for views...
Am I right?
As an example consider following working code:
#include <algorithm>
#include <ranges>
#include <vector>
#include <iostream>
enum Color {
Red,
Green,
Blue
};
struct Cat {
int age;
Color color;
};
int main() {
std::vector<Cat> cats{{.age = 10,.color=Color::Red}, {.age = 20,.color=Color::Blue}, {.age = 30,.color=Color::Green}};
auto is_red = [](const auto& cat) {return cat.color == Color::Red;};
for (const auto& cat: cats | std::views::filter(is_red)) {
std::cout << cat.age << std::endl;
}
}
Is there a way to remove the lambda and do something like:
for (const auto& cat: cats | std::views::filter(&Cat::color, Color::Red) {
note: my question is for member variable projection, but obviously in real code member function calls would be also needed.