I'm trying to familiarize with the ranges-v3
library, that will be part of the C++20 standard. To do so, I'm trying to refactor some toy code by replacing (where suitable) classic iterators and algorithms with the new available constructs. In this particular example, I can't figure out how to pass the returned iterator of a call to ranges::min_element
(which replaced a call to std::min_element
) to another function of mine which accepts a classic iterator
as parameter.
I've searched inside the documentation of the library looking for some sort of functions like smartIt2classicIt
with no success.
Here is a minimal example
void f(std::vector<int>& v, std::vector<int>::iterator it); // old function that I want to reuse
auto predicate = [](int i){ return true; }; // check function
std::vector<int> v;
// auto min_el = std::min_element(...); // old code
auto filtered_range = v | ranges::view::filter(predicate); // to avoid a dangling iterator
auto min_el = ranges::min_element(filtered_range);
f(v, min_el); // pass min_el to f: doesn't compile with the new code
At first I expected that the result of ranges::min_element
was implicitly convertible to a classic iterator, but I was wrong: the compiler returns a long error saying that cannot convert a ranges::basic_iterator bla bla bla to a std::vector bla bla bla iterator
. Given this error I deduce that ranges::min_element
indeed returns some sort of iterator, but how to use it the old way?
I see three possible solutions:
- Change the way
min_el
is passed tof
- Change the type of the second argument of
f
(possibly in a backward-compatible way) - Change both things
but I can't figure out how to implement any of them. Maybe there is another solution? I also see another possible source of troubles, since the returned iterator probably refers to filtered_range
instead of v
... Any help is welcome!