Wednesday, May 20, 2009

What are Functors or Function Objects?

A functor is an object that can behave like a function. It does so by overloading operator () (i.e., function call). It should generally implement operator () as a const member function, i.e., doesn't change the object or is stateless. It should implement cheap copy construction and assignment because temporaries are often created and should be optimized. A functor is suitable for working with template functions, such as the STL algorithms sort() and copy(), because of these properties.