What is a functor (function object) and how does it differ from a regular function?
Last updated
A functor is an object of a class overloading
operator(), so it's callable like a function but can hold state in member variables (e.g. a threshold).Each functor is a distinct type, so templates can inline its call. This often beats a function pointer.
Lambdas are sugar for compiler-generated functors (captures become members). Both are widely used as STL predicates and comparators (
std::sort,std::count_if).