Quick A: A convenient way to create a functor.
Recently on SO:
What is a lambda expression in C++11?
C++ includes useful generic functions like std::for_each and std::transform, which can be very handy. Unfortunately they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function.
#include <algorithm> #include <vector> namespace { struct f { void operator()(int) { // do something } }; } void func(std::vector<int>& v) { f f; std::for_each(v.begin(), v.end(), f); }If you only use f once and in that specific place it seems overkill to be writing a whole class just to do something trivial and one off...
Add a Comment
Comments are closed.