#include using std::cout; using std::endl; #include using std::string; #include using std::vector; #define BOOST_FCPP_ENABLE_LAMBDA #include "prelude.hpp" using namespace boost::fcpp; struct MyClass { int mem; MyClass( int x ) : mem(x) {} void save( int y ) const { cout << "Mem is " << mem << " and arg is " << y << endl; } ~MyClass() { cout << "Killing MC" << mem << endl; } }; // if save were a functoid struct YourClass { int mem; struct save_f : public c_fun_type { void operator()( YourClass* this_, int y ) const { cout << "Mem is " << this_->mem << " and arg is " << y << endl; } }; typedef full2 save_type; static save_type gen_save; typedef RT::result_type mem_save_type; mem_save_type save; YourClass( int x ) : mem(x), save( gen_save(this) ) {} ~YourClass() { cout << "Killing YC" << mem << endl; } }; YourClass::save_type YourClass::gen_save; int main() { lambda_var<1> X; string veq = "v="; cout << endl << "test1" << endl; int a[]={5,3,8,4}; std::for_each(a,a+4, lambda(X)[ &cout %out_stream% X %out_stream% '\n' ] ); cout << endl << "test2" << endl; std::for_each(a,a+4, lambda(X)[ &cout %out_stream% veq %out_stream% X %out_stream% '\n' ] ); cout << endl << "test3a" << endl; MyClass mc(0); mc.save(6); vector tests; tests.push_back( new MyClass(1) ); tests.push_back( new MyClass(2) ); std::for_each( tests.begin(), tests.end(), ptr_to_fun(&MyClass::save)(_,5) ); cout << endl << "test3b" << endl; YourClass yc(0); yc.save(6); vector tests2; tests2.push_back( new YourClass(1) ); tests2.push_back( new YourClass(2) ); std::for_each( tests2.begin(), tests2.end(), YourClass::gen_save(_,5) ); cout << endl << "test4" << endl; std::for_each( tests.begin(), tests.end(), delete_ ); std::for_each( tests2.begin(), tests2.end(), delete_ ); cout << endl << "the end" << endl; } #if 0 test1 5 3 8 4 test2 v=5 v=3 v=8 v=4 test3a Mem is 0 and arg is 6 Mem is 1 and arg is 5 Mem is 2 and arg is 5 test3b Mem is 0 and arg is 6 Mem is 1 and arg is 5 Mem is 2 and arg is 5 test4 Killing MC1 Killing MC2 Killing YC1 Killing YC2 the end Killing YC0 Killing MC0 #endif