template<class InputIt1, class InputIt2 class BinaryFunction> UnaryFunction for_each(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryFunction f) { for (; first1 != last1; ++first1, ++first2) { f(*first1, *first2); } return f; }
I sometimes - often - want to apply binary functions element-wise between two ranges. In order to do so, it would be great to have
an overload of std::for_each for binary functions.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
|
--
>
>
>
>
On Sep 18, 2013 7:07 PM, "Zhihao Yuan" <z...@miator.net> wrote:
> So that InputIterator requires decltype, ForwardIterator and
> follow ups require iterator_traits<X>::reference.
>
> But of course this breaks user code...
Errr, not really; it just takes some time to adapt to, like std::addressof.
Do you think this is something can be changed with a library issue?
--
Zhihao
On Wed, 2013-09-18 at 13:35 -0700, Bryan St. Amour wrote:
> Not quite. The variadic pack has to come at the end of the parameter
> list, so if you put the function first, then yeah that will work. But
> it breaks with the traditional order of parameters in the STL. I think
> the best you can do to support a variadic for_each is to pass the
> begin iterators in as a tuple,and specify a last iterator to constrain
> the size of the range, followed by the function. i.e.
>
> template <typename Iter, typename Func, typename... Iters>
> for_each(std::tuple<Iter, Iters...> firsts, Iter last, Func f);
One could also embrace that necessity and use
template <typename Iter, typename Func, typename... Iters>
for_each(Iter first1, Iter last1, Func f, Iters .. firsts)
with the advantage that it covers the already present case with 0 Iters
as well. This also marks the trailing firsts as lesser than the initial
one so they are not to be expected to be used for range checking.