Jump to content

Functional (C++)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tea2min (talk | contribs) at 14:50, 21 March 2012 (Mention that in the context of other programming languages the term function object tends to have a somewhat more restricted meaning.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In the context of the C++ programming language, a function object or functor is any object that can be called as if it were a function. A function object can be an object of a class that defines a function call operator but also an ordinary function or a function pointer.[1] By including ordinary functions, the definition extends the meaning of the term function object as it it understood in the context of other programming languages.

The class template std::function provided by C++11 (the most recent iteration of the C++ programming language) is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any callable target—functions, lambda expressions, bind expressions, or other function objects.

The algorithms provided by the C++ Standard Library do not require function objects of more than two arguments. Function objects that return Boolean values are an important special case. A unary function whose return type is bool is called a predicate, and a binary function whose return type is bool is called a binary predicate.

Adaptable function objects

In general, a function object has restrictions on the type of its argument. The type restrictions need not be simple, though: operator() may be overloaded or may be a member template. Similarly, there need be no way for a program to determine what those restrictions are. An adaptable function object, however, does specify what the argument and return types are, and provides nested [[typedef|typedef]]s so that those types can be named and used in programs. If a type F0 is a model of an adaptable generator, then it must define F0::result_type. Similarly, if F1 is a model of the adaptable unary function, it must define F1::argument_type and F1::result_type, and if F2 is a model of the adaptable binary function, it must define F2::first_argument_type, F2::second_argument_type, and F2::result_type. The STL provides base classes unary_function and binary_function to simplify the definition of adaptable unary functions and adaptable binary functions.

Adaptable function objects are important, because they can be used by function object adaptors: function objects that transform or manipulate other function objects. The STL provides many different function object adaptors, including unary_negate (that returns the logical complement of the value returned by a particular adaptable predicate), and unary_compose and binary_compose, which perform composition of function object.

Predefined function objects

The STL includes many different predefined function objects, including arithmetic operations (plus, minus, multiplies, divides, modulus, and negate), comparisons (equal_to, not_equal_to, greater, less, greater_equal, and less_equal), and logical operations (logical_and, logical_or, and logical_not). It is possible to perform very sophisticated operations without actually writing a new function object, simply by combining predefined function objects and function object adaptors.[1]

References

  1. ^ a b Josuttis, Nicolai M. (1999). The C++ Standard Library. Addison-Wesley. ISBN 0-201-37926-0.