range-v3 prohibits viewing temporary containers to help us avoid creating spruce. Your example shows exactly why this rule is necessary for compositions:
auto rng = src | view::transform(f) | view::join;
If view::join were to store the begin and end iterators of the temporary vector returned by f , they would be invalidated before they were used.
"That's all great, Casey, but why not choose v3 ranges, keep time ranges like this inside?"
Because performance. Just as the performance of STL algorithms is based on the requirement that iterator operations be O (1), the performance of composition compositions is based on the requirement that view operations be O (1). If the views displayed time ranges in the internal containers "behind", then the complexity of the viewing operations and, therefore, the compositions would become unpredictable.
"Good, great. Given that I understand all this wonderful design, how do I get this to work ?!"
Since the composition of the view will not save the time ranges for you, you need to dump them yourself into some kind of storage, for example:
#include <iostream> #include <vector> #include <range/v3/range_for.hpp> #include <range/v3/utility/functional.hpp> #include <range/v3/view/iota.hpp> #include <range/v3/view/join.hpp> #include <range/v3/view/transform.hpp> using T = int; std::vector<T> f(T t) { return std::vector<T>(2, t); } int main() { std::vector<T> buffer; auto store = [&buffer](std::vector<T> data) -> std::vector<T>& { return buffer = std::move(data); }; auto rng = ranges::view::ints | ranges::view::transform(ranges::compose(store, f)) | ranges::view::join; unsigned count = 0; RANGES_FOR(auto&& i, rng) { if (count) std::cout << ' '; else std::cout << '\n'; count = (count + 1) % 8; std::cout << i << ','; } }
Note that the correctness of this approach depends on the fact that view::join is an input range and, therefore, single-pass.
"This is not newbie friendly. Damn, this is not useful for experts. Why is there no support for" materializing temporary storage "in the -3 range?
Because we did not get to it - patches are welcome;)