boost::function/bind Usage Again

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

On Sun, 18 Aug 2002, Albrecht Fritzsche wrote:
funs.push_back(boost::bind(&Foo::bar, &foo, _1)); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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...
Looks good to me.
So, I would be happy if anyone could let me know if my usage includes some misuse etc...
You might consider changing boost::function<void, int> to boost::function1<void, int>, because the former syntax will be deprecated as of 1.29.0. Doug

From: "Albrecht Fritzsche" <albrecht.fritzsche@tiscali-dsl.de>
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.
participants (3)
-
Albrecht Fritzsche
-
Douglas Paul Gregor
-
Peter Dimov