Member function pointers do not include a context (as opposed to a lambda or boost::function
). To make the code work you need to replace your type definition of DoubleGetter
to:
typedef boost::function<const std::pair<double, double>(const std::string&)> DoubleGetter;
Also there is also no need to dereference the smart pointer when you supply the context (Bar
)(if you intend to do so anyway you can use the shorthand dereference operator directly):
// Pass the pointer directly to increment the reference count (thanks Aleksander)
Foo foo(s, boost::bind(&Bar::getDoubles, bar, _1));
I also noticed that you define a normal function pointer. In case you want to avoid use of boost::function completely you can use the following approach (I excluded non-altered parts):
typedef const std::pair<double, double> (Bar::*DoubleGetter)(const std::string &);
class Foo : private boost::noncopyable {
public:
explicit Foo(const std::string &s, Bar& bar, DoubleGetter dg);
// Call dg by using: (bar.*dg)(s);
};
// Instantiate Foo with:
Foo foo(s, *bar, &Bar::getDoubles);