Every time I see function taking function
as template parameter, it takes function
object by value.
E.g. from for_each reference
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn)
{
while (first!=last) {
fn (*first);
++first;
}
return fn; // or, since C++11: return move(fn);
}
1) Why is that?
There are many things you can pass inside, raw function, lambda function, std::function, functor... So object can be quite large (especially std::function or custom functor) and passing by value is not ideal. But reference says, that fn
must be function pointer or a move constructible function object
.
struct FunctorNMC //non-move-constructible
{
FunctorNMC() = default;
FunctorNMC(FunctorNMC&&) = delete;
void operator()(int a) const { std::cout << a << " "; }
};
cannot call like:
std::vector<int> v{1,2,3};
for_each(v.begin(), v.end(), FunctorNMC());
in VS2013 can be called like:
FunctorNMC tmp;
my_for_each(v.begin(), v.end(), tmp);
but IDEONE gives error.
2) Why is that? What if I have functor, which for some reason don't support move
?
3) Shouldn't be function taken as something like Function && fn
?
4) Can be function object passed by value (without moving)?
IDEONE project to play around if needed.