[function] How to use function with templated member functions

Hi there, I would like to boost::function with a templated member
function. Please consider the following code:
struct my_reader
{
my_reader(bool full)
{
if( full )
{
_read_function = &my_reader::read_full<int>;
}
else
{
_read_function = &my_reader::read_partial<int>;
}
}
void read()
{
_read_function(this);
}
private:
template< typename T >
void read_full()
{
std::cout << "reading full image" << std::endl;
}
template< typename T >
void read_partial()
{
std::cout << "reading partial image" << std::endl;
}
boost::function

Hi there, I would like to boost::function with a templated member function. Please consider the following code:
[snip]
This doesn't compile with Visual Studio 2010 since the compiler cannot:
"cannot convert from 'overloaded-function' to 'boost::function<Signature>'"
Is there is way to make this happen?
Your code compiles fine for me on GCC 4.6. I'm not sure why it doesn't work with Visual Studio. Perhaps you can try wrapping the function pointers with boost::mem_fn(), like this: _read_function = boost::mem_fn(&my_reader::read_full<int>); Maybe that helps. Regards, Nate

Hi Nathan,
Your code compiles fine for me on GCC 4.6. I'm not sure why it doesn't work with Visual Studio.
Interesting. I was just thinking that my syntax is wrong.
Perhaps you can try wrapping the function pointers with boost::mem_fn(), like this:
_read_function = boost::mem_fn(&my_reader::read_full<int>);
Maybe that helps.
Yes, that helped! Thanks a lot. Regards, Christian

struct my_reader { my_reader(bool full) { if( full ) { _read_function = &my_reader::read_full<int>; } else { _read_function = &my_reader::read_partial<int>; } }
void read() { _read_function(this); }
private:
template< typename T > void read_full() { std::cout << "reading full image" << std::endl; }
template< typename T > void read_partial() { std::cout << "reading partial image" << std::endl; }
boost::function
_read_function; }; int main(int argc, char* argv[]) { my_reader r(false); r.read();
return 0; }
This doesn't compile with Visual Studio 2010 since the compiler cannot:
"cannot convert from 'overloaded-function' to 'boost::function<Signature>'"
Is there is way to make this happen?
http://www.boost.org/doc/libs/1_48_0/libs/bind/bind.html#with_member_pointer...
participants (3)
-
Christian Henning
-
Igor R
-
Nathan Ridge