Boost logo

Boost Users :

From: Albrecht Fritzsche (albrecht.fritzsche_at_[hidden])
Date: 2002-08-18 03:21:28


Here I want to extend simple callbacks so that these callbacks take an
argument

void fun(int i) {
   std::cout << "Hi fun " << i << std::endl;
}
struct Foo {
   void bar(int i) { std::cout << "Hi bar " << i << std::endl; }
};
template <class Value>
struct CallFunction {
   CallFunction(const Value& v) : mValue(value) {} // defined outside
   void operator()(const boost::function<void, Value>& fun) {
     if (fun) fun(mValue);
   }
   const Value& mValue;
};

int main() {
   std::vector< boost::function<void, int> > funs;
   Foo foo;

   funs.push_back(&fun);
   funs.push_back(boost::bind(&Foo::bar, &foo, _1));
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   int value = 7;
   std::for_each(funs.begin(), funs.end(), CallFunction<int>(value));

   return 0;
}

Is this the right usage of the /_1/ here? This code runs (at
least on my machine:-), but I'm quite unsure about this place
holder. And since the online docu...

Ah, now I see - in the online doc there are some links missing.
In the files section not only the bind_template.hpp link refers
to that very file but also the arg.hpp and placeholders.hpp links
and many more. Having checked now the sources makes not everything
clear, but at least clearer.

So, I would be happy if anyone could let me know if my usage includes
some misuse etc...

Thanks
Ali


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net