#include #include #include #include int cmm_pragma_embeddedfns_on(); int cmm_pragma_detailedparse_on(); int cmm_pragma_newdeclsyntax_on(); struct Element { int x; int y; void Output( std::ostream* out) const { (*out) << "x=" << x << ", y=" << y << "\n"; } /* Using a pointer rather than reference because otherwise bind2nd gets into trouble with references to references. */ }; template< class T> void Fn( const T& item) { item.Output( &std::cout); } int main() { std::vector< Element> elements(10); for ( int i=0; i<10; ++i) { elements[i].x = i; elements[i].y = i*i; } // Here are some different ways to output all items in `elements': // Using standard binders etc std::for_each( elements.begin(), elements.end(), std::bind2nd( std::mem_fun_ref(&Element::Output), &std::cout) ); // Using an explicit external functin std::for_each( elements.begin(), elements.end(), Fn ); // Using an inline function body (Cmm extension) std::for_each( elements.begin(), elements.end(), void () ( Element& item) { item.Output( &std::cout); } ); // Using an inline function body with new-style declaration syntax (Cmm extension) std::for_each( elements.begin(), elements.end(), foo: ( item: &Element) void { item.Output( &std::cout); } ); return 0; }