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
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
From: "Albrecht Fritzsche"
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
& fun) { if (fun) fun(mValue); } const Value& mValue; }; int main() { std::vector< boost::function
> 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