Takes fn by forwarding ref. in for_each_pair, resolves #4148

This commit is contained in:
Daniel J. Hofmann 2017-06-13 12:33:03 +02:00 committed by Patrick Niklaus
parent 27ed69b08f
commit b68d79407e

View File

@ -3,6 +3,7 @@
#include <iterator> #include <iterator>
#include <numeric> #include <numeric>
#include <utility>
namespace osrm namespace osrm
{ {
@ -12,11 +13,11 @@ namespace util
// TODO: check why this is not an option here: // TODO: check why this is not an option here:
// std::adjacent_find(begin, end, [=](const auto& l, const auto& r){ return function(), false; }); // std::adjacent_find(begin, end, [=](const auto& l, const auto& r){ return function(), false; });
template <typename ForwardIterator, typename Function> template <typename ForwardIterator, typename Function>
Function for_each_pair(ForwardIterator begin, ForwardIterator end, Function function) void for_each_pair(ForwardIterator begin, ForwardIterator end, Function &&function)
{ {
if (begin == end) if (begin == end)
{ {
return function; return;
} }
auto next = begin; auto next = begin;
@ -24,19 +25,18 @@ Function for_each_pair(ForwardIterator begin, ForwardIterator end, Function func
while (next != end) while (next != end)
{ {
function(*begin, *next); std::forward<Function>(function)(*begin, *next);
begin = std::next(begin); begin = std::next(begin);
next = std::next(next); next = std::next(next);
} }
return function;
} }
template <class ContainerT, typename Function> template <class ContainerT, typename Function>
Function for_each_pair(ContainerT &container, Function function) void for_each_pair(ContainerT &container, Function &&function)
{ {
using std::begin; using std::begin;
using std::end; using std::end;
return for_each_pair(begin(container), end(container), function); for_each_pair(begin(container), end(container), std::forward<Function>(function));
} }
} // namespace util } // namespace util