Il 10/09/2015 22:14, Marcel Mueller ha scritto:
> for (int num : even_numbers(vector<int>({ 1,5,3,2,3,6,8 })))
what's wrong with:
for (int num : even_numbers({ 1,5,3,2,3,6,8 }))
anyway:
> The function even_numbers just picks the even number from its input.
> But it creates a collection with all the results. No problem in this
> simple example, but when the input is large transient data instead of
> vector<> this is no longer desirable.
>
> So I would prefer to return a virtual container that just supports
> input iteration and returns the requested results on the fly.
I am not sure I understood your question.
Are you talking about using/returning (a lighter) vector of wrappers,
similar to std::vector<std::reference_wrapper<int>>, for example:
http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
(see the examples there)
Also, although I do not clearly see what you are trying to achieve,
consider this alternative approach:
template <class F, class... Args>
void for_each_argument(F f, Args&&... args) {
std::array<int, sizeof...(Args)>{(f(std::forward<Args>(args)), 0)...};
}
for_each_argument([](int num) {
if (!(num&1)) printf("%i\t", num);
}, 1, 5, 3, 2, 3, 6, 8);