Stl Function Objects

One of the definitions of FunctorObjects is...

In the CeePlusPlus StandardTemplateLibrary, a "FunctorObject" is an instance of a class that implements "operator()." As such, its usage is syntactically indistinguishable from a FunctionPointer -- a property that lets FunctorObjects and FunctionPointers to be used interchangeably in template-based GenericAlgorithms?.

See: FunctorObject

Benefits of using objects instead of FunctionPointers:

"What are StlFunctionObjects?" / how to create and use them...


If C++ had GarbageCollection, a FunctionObject could be used to give C++ true higher-order functions (though with an ugly syntax). One could create and return a FunctionObject (or a reference/pointer to same) in a HigherOrderFunction, use it as necessary, and let the GC dispose of it.

Without GC, though; this use becomes problematic as the user must then manually free the function object. Given the typically usage patterns of function objects created in such fashion (they often get passed around, or stuffed in a data structure, and die well after they are created), manual reclaimation of these is a RoyalPain (i.e. error-prone and likely to leak memlry). Workarounds:

CategoryObjectFunctionalPatterns CategoryCpp CategoryFunctionalProgramming

EditText of this page (last edited April 1, 2008) or FindPage with title or text search