boost bind class function pointer


class Foo { double f1( int x, std::string s1 ); double f2( int x, SomeClass s2 ); }

I want to be able to bind Foo.f1's s1 without an instance of foo to create in essense

typedef double (Foo::* MyFooFunc)( int ) MyFooFunc func1 = boost::bind( &Foo::f1, _1, _2, "some string" ); MyFooFunc func2 = boost::bind( &Foo::f2, _1, _2, SomeClass );

Then I pass func1 and func2 as parameters to other functions, inside which Foo is finally bound:

void SomeOtherFunction( MyFooFunc func ) { Foo foo; boost::function< double (int) > finalFunc = boost::bind( func, foo, _1 ); }

Questions: Is this possible? If yes, 1) how to achieve it? 2) What's the declaration of MyFooFunc?


typedef double (Foo::* MyFooFunc)( int ); MyFooFunc func1 = boost::bind( &Foo::f1, _1, _2, "some string" );

The result of boost::bind is not a pointer to member, so func1 cannot be initialized as such on the second line. The result of boost::bind is an unspecified type (which will depend on the parameters). If you're using C++0x, the simplest way to name the result of a call to bind is to use auto:

auto func1 = boost::bind( &Foo::f1, _1, _2, "some string" );

Another simple way (not restricted to C++03) is simply to not name the result, but to use it on the spot:

SomeOtherFunction(boost::bind(&Foo::f1, _1, _2, "some string"));

Or, you can use type-erasure to store the result of boost::bind into a boost::function, which you seem to be familiar with. boost::function<double(Foo&, int)> is a possibility but not the only choice.

<hr />

We now need to find the appropriate signature for SomeOtherFunction: again, a pointer to member can't be initialized from the result of a call to boost::bind, so void SomeOtherFunction(MyFooFunc func); won't work. You can make the function a template instead:

template<typename Func> void SomeOtherFunction( Func func ) { Foo foo; boost::function< double (int) > finalFunc = boost::bind( func, foo, _1 ); }

If a template is not preferrable, then you must use some kind of type-erasure such as, again, boost::function.

void SomeOtherFunction(boost::function<double(Foo&, int)> const& func);

(once again other boost::function types are possible depending on details such as passing a ref-to-const as opposed to a ref-to-non-const)


Try this:

boost::bind(&Foo::f1, object, _1, _2);

object is an instance of class Foo. _1 and _2 are the argument placeholders.


  • What's the easiest way to print the address of a member function?
  • Wrapping a function with an indeterminate number of parameters in F#
  • How to split a text file into multiple columns with Spark
  • Mongodb: compute a value before using it as connectFromField in an aggregate
  • C++: Undefined symbols when loading shared library with dlopen()
  • How to determine if an array has consecutive integers and if so, how many?
  • List or scalar context when returning an array or list with a single element
  • Spark DataFrame equivalent to Pandas Dataframe `.iloc()` method?
  • Is it possible to refresh a div on a radio button click?
  • What is #:: method
  • jaxb xsd prefix package names
  • How to restrict number of concurrent processes?
  • Intellij Idea Terminal shortcut not working
  • Selenium and Google - How do you use cookies?
  • Use neo4j server instead of embedded mode
  • Calling C function from lua
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Creating a DropDownList
  • Who propagate bugfixes across branches (corporate development)?
  • How to remove a SwiftyJSON element?
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Time complexity of a program which involves multiple variables
  • C++ and preprocessor macros: variadic type
  • Scrapy recursive link crawler
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spring security and special characters
  • output of program is not same as passed argument
  • Does CUDA 5 support STL or THRUST inside the device code?
  • JSON with duplicate key names losing information when parsed
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Why winpcap requires both .lib and .dll to run?
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • C# - Getting references of reference
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?