Created
March 28, 2012 09:00
-
-
Save sithhell/2224918 to your computer and use it in GitHub Desktop.
Complete Bind solution with protect and a functor
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> | |
template <typename F> | |
void g(F const & f) | |
{ | |
f(); | |
} | |
struct g_trampoline | |
{ | |
typedef void result_type; | |
template <typename F, typename T> | |
result_type operator()(F const & f, T const &t) const | |
{ | |
invoke(boost::bind(f, t)); | |
} | |
template <typename F> | |
result_type invoke(F const & f) const | |
{ | |
void (*gg)(F const &) = g; | |
gg(f); | |
} | |
}; | |
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; | |
std::for_each( v.begin( ), v.end( ), boost::bind(g_trampoline(), 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