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

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(42,69);

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

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

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

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

  return 0;
};

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

commented: Nice! I need to look into that! +4

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.