struct Widget {
int weight;
std::string name;
};
auto get_widgets() -> std::vector<Widget>
And I want to get a vector of the widget names. That seems like I should want this:
auto names = get_widgets() | actions::transform(&Widget::name);
But actions::transform doesn't let me change the element type, it's an in-place transform. So then it seems I might want this:
auto names = get_widgets() | views::transform(&Widget::name) | ranges::to<std::vector>();
But this doesn't work because get_widgets() isn't a viewable_range, despite it being consumed by this operation and so being perfectly safe. This leads me to having to write either:
auto widgets = get_widgets();
auto names = widgets | views::transform(&Widget::name) | ranges::to<std::vector>();
which takes an extra line and introduces an extra name, or add a helper somewhere like:
auto as_lvalue = [](auto&& v) -> auto& v { return v; };
to force get_widgets() to behave like a viewable_range:
auto names = as_lvalue(get_widgets())
| views::transform(&Widget::name)
| ranges::to<std::vector>();
Which just seems hacky.
It seems like there should be a better solution for this kind of transformation.
I have the following setup:
And I want to get a vector of the widget names. That seems like I should want this:
But
actions::transform
doesn't let me change the element type, it's an in-place transform. So then it seems I might want this:But this doesn't work because
get_widgets()
isn't aviewable_range
, despite it being consumed by this operation and so being perfectly safe. This leads me to having to write either:which takes an extra line and introduces an extra name, or add a helper somewhere like:
to force
get_widgets()
to behave like aviewable_range
:Which just seems hacky.
It seems like there should be a better solution for this kind of transformation.