Hey all,

I'm attempting to bind a function, then store it in a map. Later I want to call that function with parameters that I don't yet have when I bind it.

So this is the code
boost::bind(&State::setBufferSourcePlaying, &::top->dsp->getState(), 0, 1 )

Currently I'm not binding any "dynamic" data, ie 0 and 1 passed to setBufferSourcePlaying aren't changing. And that's the functionality I'm after...

Any Boost guys there know how to do this?
Thanks for reading, -Harry

5 Years
Discussion Span
Last Post by harryhaaren

Yeah, you use a thing called "placeholders". In Boost, these are _1, _2, etc. The number represents which parameter is passed there, so you can even change the order if you want, and you can fix some parameters and leave others to be passed to the function when it is called. As so:

#include <boost/bind.hpp>
#include <iostream>

void func(int i, int j) {
  std::cout << i << " " << j << std::endl;

int main() {
  boost::function<void(int,int)> f;

  f = boost::bind(&func, _1, _2);

  f = boost::bind(&func, _2, _1);

  boost::function<void(int)> f2;

  f2 = boost::bind(&func, 0, _1);

  f2 = boost::bind(&func, _1, 0);

  return 0;

Read the Boost.Bind docs for more examples of what you can do.

Votes + Comments
Nice! I need to look into that!

Thanks that's exactly what I was looking for. Due to the number & type of the arguments having to be defined in the storage container (I have a couple of hunderd bound functions in a vector) it's pretty clumsy to store each function in a different vector.

I'm thinking of trying to get a pointer to the boost::bind<void()> "functor", and then change the parameters after that... it seems hard over complicated though.

Perhaps this is the place for a little encapsulation in a BoundFunction class. I'll keep working on it.
Thanks Mike! -Harry

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.