What is the difference between std::mem_fun and std::mem_fn? Why is the naming so confusing?
Boost’s documentation says that std::mem_fn can replace std::mem_fun in most cases. So in what situation would you still use std::mem_fun?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
std::mem_funis deprecated.std::mem_fncan do everything it does, and it does it more conveniently. The relation between the two is the same as the relation betweenstd::bind1st/std::bind2ndand the C++11std::bind. Bothstd::mem_fnandstd::bindwere developed and mastered afterstd::bind1standstd::mem_funwere made into the C++98 Standard. So that means we had to wait until C++11 to properly replace the old stuff with the superior alternatives.For instance,
std::mem_funcan only deal with member functions that take one or no argument.std::mem_fnis variadic and can deal with members that take any number of arguments.You also need to pick between
std::mem_funandstd::mem_fun_refdepending on whether you want to deal with pointers or references for the class object (respectively).std::mem_fnalone can deal with either, and even provides support for smart pointers.The documentation of
boost::mem_fnexplains when to usestd::mem_fun, and put simply that’s when you need to operate with code that expectsstd::mem_fun, or that expects adaptable functors (which is an obsolete notion* from C++03). For those cases you wouldn’t be able to plug instd::mem_fneither, so there you have it: you would usestd::mem_funfor legacy.*: I mean by that that new code shouldn’t rely on the C++03 protocol of having e.g.
result_typemember types (it’s more customary to use the new traits likestd::result_of) — the new facilities likestd::bind/std::mem_fndo in fact provide those members if they would have been present in equivalent C++03 code. I leave it to you to figure out whether you should update old code that relies on adaptable functors withstd::mem_fnby relying on this behaviour.