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

I want to provide a member function for the "comp" parameter for an STL algorithm such as lower_bound (..., Compare comp). The comp () function refers to a non-stationary element, so it must be a non-stationary member, but the type of non-static pointer of a user function is different from the type of a regular function pointer.

What is the best way to solve this problem?

+4
source share
3 answers

This is the most common use of std::mem_fun and std::mem_fun_ref . These are patterns that create functors that call the specified member function. TR1 adds std::tr1::bind , which is also useful and universal (and if you don't have TR1, then it is based on Boost::bind ). C ++ 0x will include std::bind in the standard library (almost unchanged with TR1).

+7
source

It looks like you want something like boost::bind to bind a member function pointer to an instance of this class.

Could you tell us a little more about what you are trying to do? Code example etc.?

+6
source
 #include<tr1/functional> 

and use mem_fn ()

+1
source

Source: https://habr.com/ru/post/1316244/


All Articles