Created
March 28, 2012 08:59
-
-
Save sithhell/2224915 to your computer and use it in GitHub Desktop.
Complete Bind solution with protect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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