pointer_unary_function and class member functions

Hello to everybody,
I have a compile problem that relates to the use of std::pointer_unary_function with a class member function. There is a simple example :

There is MyClass.h
1
2
3
4
5
6
7
8
#include <functional>


class MyClass {
public:
	double myFunInMyClass(double v);
	void ptrFunHandle();
};

..and MyCLass.cpp
1
2
3
4
5
6
7
8
9
#include "MyClass.h"

double myFunOut(double v){return 0;}
double MyClass::myFunInMyClass(double v){return 0;}
void MyClass::ptrFunHandle()
{
	//std::pointer_to_unary_function<double, double> ptr_fun(this->myFunInMyClass);//does NOT compile
	std::pointer_to_unary_function<double, double> ptr_fun(myFunOut);//does compile
}


When I use the first line of ptrFunHandle function, I have the message :

erreur: no matching function for call to ‘std::pointer_to_unary_function<double, double>::pointer_to_unary_function(<unresolved overloaded function type>)’


I tried many other way to points to the function : "this->MyCLass::myFunInMyClass" rather than "this->myFunInMyClass", even "myFunInMyClass". And these attempts did not succeed. It seems natural that the type of the class member function is not known, because the class is being compiled (however, g++ could know it thanks to the .h file)..

Do you know how to solve this ?
Or do you know some other way to use a pointer on a class member function in some function pointer (i.e. not necessarily pointer_to_unary_function) ? Something clean please :-)
Thanks in advance for all answers..
Last edited on
Methods are not like other functions. The main difference is that they have access to the implicit this parameter.
Yes, the two types of function are not compatible, alas.
http://www.newty.de/fpt/index.html
Use boost::bind and boost::function instead. They are much more flexible and powerful.

EDIT:

www.boost.org

1
2
3
4
5
6
7
8
#include <boost/bind.hpp>
#include <boost/function.hpp>

// Assumes this line is in a member function of MyClass to be able to use "this" pointer
boost::function< double( double )> fn( boost::bind( &MyClass::myFunInMyClass, this, _1 ) );

// Call the function
std::cout << fn( 4.0 ) << std::endl;
Last edited on
Ok thanks you !
boost::bind and boost::function work for me !
Excellent!

If you are familiar with/have used <functional>, then you might also be interested in reading up
on the boost::lambda library. In some cases*, you can avoid the extra named function altogether
by using a lambda expression. For example:

1
2
3
4
5
6
7
8
typedef std::map<char, char> CharMap;

CharMap charMap;

//...
std::for_each( charMap.begin(), charMap.end(),
    std::cout << (&boost::lambda::_1->*&CharMap::value_type::first) << " -> "
                 << (&boost::lambda::_1->*&CharMap::value_type::second) << '\n' );


Outputs the entire contents of the map, without having to write operator<< for std::pair<char, char>.

*lambda expressions can get quite complicated, and despite lambda support for doing almost anything (if-else, switch, while, try, catch, etc), often times for readability's sake it is better to create a named function anyway.
Topic archived. No new replies allowed.