I want to make a function that works like np.arange()
. With range-v3, the code is
auto arange(double start, double end, double step){
assert(step != 0);
const auto element_count = static_cast<int>((end - start) / step) + 1;
return ranges::views::iota(0, element_count) | ranges::views::transform([&](auto i){ return start + step * i; });
}
and to use it,
auto range = arange(1, 5, 0.5);
for (double x : range){
std::cout << x << ' '; // expect 1 1.5 2 2.5 3 3.5 4 4.5 5
}
However, the result told me a dummy value. I think the lifetime of returned range object is expired, and I found that by making them to vector can pass the result well. (And it will cause overhead for constructing vector.)
Is there any way to return range itself without expired lifetime ?