Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to "cast" a member function pointer to a function pointer in C++?

I want to provide a member function for the "comp" parameter of an STL algorithm like lower_bound( ..., Compare comp ). The comp() function accesses a non-static member field so it must itself be a non-static member but the type of a non-static member function pointer is different from that of an ordinary function pointer.

What is the best way around this problem?

like image 453
zoo Avatar asked Dec 10 '22 14:12

zoo


1 Answers

This is the most common use of std::mem_fun and std::mem_fun_ref. They're templates that create functors that invoke the specified member function. TR1 adds an std::tr1::bind that's also useful and more versatile (and if you don't have TR1 available, that's based on Boost::bind). C++0x will include std::bind in the standard library (virtually unchanged from TR1).

like image 141
Jerry Coffin Avatar answered Dec 12 '22 05:12

Jerry Coffin