The Functor Package
Introducing the Functor Package
A functor, also known as a function object, is an object type that can be called as if it were a function. A functor acts as an adapter, providing a common interface for invoking a callable object without regard to how that callable object is implemented. The Functor package provides a convenient, flexible, and powerful way to encapsulate callable types, including functions, lambda expressions, and other callable types.
Along with its support for functors, the Functor package also provides a powerful API to support parameter binding. Parameter binding allows for the creation of a binder object that encapsulates both a callable object as well as some or all of the parameters required to invoke that object.
The functor's interface can be independent of the encapsulated callable object. Because the information for invoking the callable type is encapsulated in an object, the invocation can be deferred or delegated. Software components can pass a functor as a parameter or return value, like any other object.
Functors make it straightforward to write a tool that can accept and carry out any arbitrary piece of work. Instead of trying to handle a variety of unknown function signatures, such a tool takes one type of functor whose arguments and return value fit the tool's own requirements. Each functor handles the details of adapting its encapsulated function to those requirements.
The Functor package provides the following features:
*Functors are type flexible. Their parameters are templates, ensuring consistent interfaces and code generation by the compiler.
*Functors are object-oriented and generic. You can use them in any situation, like any other object.
*Functors implement a consistent interface for different callable types. The encapsulated callable object need not match the functor's signature exactly, as long as the types are convertible.
*Functors are convenient. Once you abstract out the notion of a function to a functor object, you can reuse the functor in any number of situations.
*Functors are an easy way to exchange information about functions, because you are dealing with an object, rather than a function pointer.
*Functors are compile-time type safe. The compiler does the type conversions. If you try to make a functor out of a function that is not compatible with the functor type, you get a compiler error.