Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-08-19 11:53:19


From: "Albrecht Fritzsche" <albrecht.fritzsche_at_[hidden]>
> 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...

Yes, it looks correct.

bind(&Foo::bar, &foo, _1)

generates the following "function":

void function(_1)
{
    return ((&foo)->*(&Foo::bar))(_1);
}

> 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.

Fixed, thanks.


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