[phoenix] - std::for_each - How to call member variable that is a boost::function?

I'm a little confused on the correct way to lazily call a boost::function in a phoenix expression. For example, this doesn't do anything: namespace bp = boost::phoenix; namespace bpa = bp::arg_names; struct Foo { boost::function< void() > callback; Foo(boost::function< void() > const& callback) : callback(callback) { } }; main() { std::list< Foo > foos; foos.push_back(Foo(std::cout << bp::val(1) << '\n')); foos.push_back(Foo(std::cout << bp::val(2) << '\n')); foos.push_back(Foo(std::cout << bp::val(3) << '\n')); std::for_each( foos.begin(), foos.end(), bp::bind(&Foo::callback, bpa::arg1)); } If I wrap the bind(...) in another bind(...) it'll work, but is that the right way? I'm not binding anything, I just want the callback called. --- Aaron Wright

namespace bp = boost::phoenix; namespace bpa = bp::arg_names;
struct Foo { boost::function< void() > callback;
Foo(boost::function< void() > const& callback) : callback(callback) { } };
main() { std::list< Foo > foos;
foos.push_back(Foo(std::cout << bp::val(1) << '\n')); foos.push_back(Foo(std::cout << bp::val(2) << '\n')); foos.push_back(Foo(std::cout << bp::val(3) << '\n'));
std::for_each( foos.begin(), foos.end(), bp::bind(&Foo::callback, bpa::arg1)); }
If I wrap the bind(...) in another bind(...) it'll work, but is that the right way? I'm not binding anything, I just want the callback called.
FWIW, the following code compiles in MSVC10 with boost 1.53
(basically, your code in form of sscce):
#include

Yes it does compile, but it doesn't do anything. I'm looking for it to
print out the numbers. I need the callback to be called.
---
Aaron Wright
From: Igor R
namespace bp = boost::phoenix; namespace bpa = bp::arg_names;
struct Foo { boost::function< void() > callback;
Foo(boost::function< void() > const& callback) : callback(callback) { } };
main() { std::list< Foo > foos;
foos.push_back(Foo(std::cout << bp::val(1) << '\n')); foos.push_back(Foo(std::cout << bp::val(2) << '\n')); foos.push_back(Foo(std::cout << bp::val(3) << '\n'));
std::for_each( foos.begin(), foos.end(), bp::bind(&Foo::callback, bpa::arg1)); }
If I wrap the bind(...) in another bind(...) it'll work, but is that the right way? I'm not binding anything, I just want the callback called.
FWIW, the following code compiles in MSVC10 with boost 1.53
(basically, your code in form of sscce):
#include

On Tue, Apr 30, 2013 at 9:00 AM,
I'm a little confused on the correct way to lazily call a boost::function in a phoenix expression. For example, this doesn't do anything:
namespace bp = boost::phoenix; namespace bpa = bp::arg_names;
struct Foo { boost::function< void() > callback;
Foo(boost::function< void() > const& callback) : callback(callback) { } };
main() { std::list< Foo > foos;
foos.push_back(Foo(std::cout << bp::val(1) << '\n')); foos.push_back(Foo(std::cout << bp::val(2) << '\n')); foos.push_back(Foo(std::cout << bp::val(3) << '\n'));
std::for_each( foos.begin(), foos.end(), bp::bind(&Foo::callback, bpa::arg1)); }
If I wrap the bind(...) in another bind(...) it'll work, but is that the right way? I'm not binding anything, I just want the callback called.
Well, excusing my indexing into a std::list for the moment, bp::bind(&Foo::callback, bpa::arg1)(foos[0]) just evaluates to foos[0].callback, i.e., it accesses the callback member, but doesn't do anything with it. The part I'm not sure about offhand is what the best way would be to then invoke that member with empty parentheses. It's non-obvious where another wrapping bind should go and why it works :( I would've coded or dug up some apply function object that evaluates its argument and utilized that struct apply { template< class T > void operator()(T f) { f(); } }; but there's probably a better way. - Jeff

"It's non-obvious where another wrapping bind should go and why it works"
I just did it like so:
std::for_each(
foos.begin(),
foos.end(),
bp::bind(bp::bind(&Foo::callback, bpa::arg1)));
But this seems silly as I'm not binding anything in the outer bind.
---
Aaron Wright
From: "Jeffrey Lee Hellrung, Jr."
participants (3)
-
Aaron_Wright@selinc.com
-
Igor R
-
Jeffrey Lee Hellrung, Jr.