std::*fun

This topic is 4678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am reading through Josuttis' STL book, and want to make sure I understand something correctly. mem_fun_ptr [edit: er mem_fun_ref, could they make any worse names?] makes a function object which is essentially called like object.functor(); mem_fun makes a function object which is essentially called like object->functor(), and ptr_fun makes a function object which is called like functor()? And that is the only practical difference between these?

Share on other sites
It's mem_fun and mem_fun_ref, they are actually free-functions, they are just convenience functions that deduces types from function arguments that creates functor instances (its all inlined away) think std::make_pair same thing.

The difference between the two is mem_fun is for when your storing pointers in containers, the definition of the over-loaded function call operator would be something like this:

(obj->*mem_func)(optional_argument);

and for containers that store by value then you use mem_fun_ref, the definition of the over-loaded function call operator would be something like this:

(obj.*mem_func)(optional_argument);

That should make the difference alittle more obvious.

When you come to the standard library binders you will notice that there incorrect and it's not an implementation issue!. Boost binders & member function adaptors over come these issues including mem_fun/mem_fun_ref differences and are being considered to be included into the standard.