Skip to content

Instantly share code, notes, and snippets.

@sithhell
Created March 28, 2012 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sithhell/2224915 to your computer and use it in GitHub Desktop.
Save sithhell/2224915 to your computer and use it in GitHub Desktop.
Complete Bind solution with protect
#include <boost/assign/std/vector.hpp>
#include <boost/bind.hpp>
#include <boost/bind/protect.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
void g(boost::function<void(int)> const &f, int i) {
f(i);
}
void f(int x) {
std::cout << "x is " << x << std::endl;
}
using namespace boost::assign;
int main( )
{
std::vector<int> v;
v += 0,1,2,3,4,5,6;
// 'protect' protects the bound function to be
// evaluated. As such, we need to somehow safe the first
// argument, and pass it to f again on the calling site g
std::for_each( v.begin( ), v.end( ),
boost::bind(g,
boost::protect(boost::bind(f, _1)),
_1)
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment