template <typename Collection,typename unop>
void for_each(Collection col, unop op){
std::for_each(col.begin(),col.end(),op);
}
...I like the pass-by-value syntax here, but it just won't work with the standard library, at least not the one in C++14. If you pass a vector into this function, you'll be creating a copy of the vector (!), then operating on the copy of the vector (!!). Then there is the question of what for_each does if it isn't manipulating state or performing I/O. In either case, you would not want an 'unop', you would want some sort of monad.For a more complete perspective on this sort of thing, I recommend Eric Neibler's blog and Bartosz Milewski's blog. Here's somewhere to start. You'll find Neibler's blog linked from there:
http://bartoszmilewski.com/2014/10/17/c-ranges-are-pure-mona...