You're mixing deferred execution with immediate evaluation:
boost::ref(myResults).get().insert(_1)
Here, boost::ref(myResults)
is not lazy, so .get()
isn't either. The type of boost::ref(myResults).get()
is just std::set<long> &
, and that type's insert
member function doesn't have an overload that takes a Boost Lambda placeholder.
I'm not well-versed in Boost Lambda (anymore) because I've moved to it's successor library, Boost Phoenix. Here's a 1-to-1 translation with fixes: Live On Coliru
#include <boost/phoenix.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <set>
template <typename Action>
void work( Action doAction ) {
long results[] = { 2, 5, 4 };
BOOST_FOREACH( long r, results )
doAction( r );
}
bool cond( long r ) { return r % 2 == 0 ; }
int main() {
namespace phx = boost::phoenix;
using namespace phx::placeholders;
std::set<long> myResults;
work(
if_(phx::bind(cond, _1)) [ phx::insert(phx::ref(myResults), _1) ] );
BOOST_FOREACH( long r, myResults )
std::cout << r << "\n";
}
Prints
2
4
I'd suggest to look at Phoenix function adaptation, to avoid the bind expressions: