std::for_each requires a function that accepts a single parameter of some type and returns void. However, its implementation actually doesn't require a type of
std::function to be passed:
1 2 3 4 5 6 7 8
|
template<class _InIt,
class _Fn1> inline
_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
_DEBUG_RANGE_PTR(_First, _Last, _Func);
_For_each_unchecked(_Unchecked(_First), _Unchecked(_Last), _Func);
return (_Func);
}
|
Notice the
_Fn1. It has no constraints and it can accept just about
anything. Anything can be passed in, like so:
std::for_each(v.begin(), v.end(), this);
However, I get a compiler error (
error C2064: term does not evaluate to a function taking 1 arguments) when trying to compile it, and knowing what for_each does, I understand why. But without further knowledge, until compilation, there is no way to know what kind of function it expects without going deep into the implementation code for for_each.
So I wonder: why didn't they use
std::function as a type for the third parameter in std::for_each? Is it because of some limitations of templates? Or is it simply preferable for some other reason?