This is because generic code that expects a UnaryFunction or BinaryFunction is called directly using regular call syntax. Therefore, to select an arbitrary algorithm, such as for_each , it could be implemented as follows:
template<class InputIt, class UnaryFunction> UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f) { for (; first != last; ++first) { f(*first);
If you called for_each() with &Item::Foo , the code tries to call (&Item::Foo)(x) , which is poorly formed, because for member pointers you need to write (x.*&Item::Foo)() . This means that the syntax difference mem_fn to solve is: mem_fn deals with the syntax for calling member pointers so that you can use all the algorithms with member pointers, as well as functions and function objects. You cannot have for_each(v.begin(), v.end(), &Item::Foo) , but you can have for_each(v.begin(), v.end(), mem_fn(&Item::Foo))
This works fine in std::bind() (and std::thread and std::function and ...) initially, since they all have explicit processing of pointers to elements separately. And since DoBinding() itself calls std::bind() , in this case there is no reason for std::mem_fn .
There is a suggestion to get rid of this syntax difference: P0312 .
Barry source share