SGI Logo


unary_compose<AdaptableUnaryFunction1,AdaptableUnaryFunction2>

Categories: functors, adaptors Component type: type

Description

Unary_compose is a function object adaptor. If f and g are both Adaptable Unary Functions, and if g's return type is convertible to f's argument type, then unary_compose can be used to create a function object h such that h(x) is the same as f(g(x)). [1] As with other function object adaptors, the easiest way to create a unary_compose is to use the helper function compose1. It is possible to call unary_compose's constructor directly, but there is usually no reason to do so.

Example

Calculates the negative of the sines of the elements in a vector, where the elements are angles measured in degrees. Since the C library function sin takes its arguments in radians, this operation is the composition of three operations: negation, sin, and the conversion of degrees to radians.
vector<double> angles;
vector<double> sines;
const double pi = 3.14159265358979323846;
...
assert(sines.size() >= angles.size());
transform(angles.begin(), angles.end(), sines.begin(),
          compose1(negate<double>(),
                   compose1(ptr_fun(sin),
                            bind2nd(multiplies<double>(), pi / 180.))));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. The unary_compose class is an SGI extension; it is not part of the C++ standard.

Template parameters

Parameter Description Default
AdaptableUnaryFunction1 The type of the first operand in the function composition operation. That is, if the composition is written f o g [1], then AdaptableUnaryFunction1 is the type of the function object f.  
AdaptableUnaryFunction2 The type of the second operand in the function composition operation. That is, if the composition is written f o g [1], then AdaptableUnaryFunction1 is the type of the function object g.  

Model of

Adaptable Unary Function

Type requirements

AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must both be models of Adaptable Unary Function. AdaptableUnaryFunction2::result_type must be convertible to AdaptableUnaryFunction1::argument_type.

Public base classes

unary_function<AdaptableUnaryFunction2::argument_type,
               AdaptableUnaryFunction1::result_type>

Members

Member Where defined Description
argument_type Adaptable Unary Function The type of the function object's argument: AdaptableUnaryFunction2::argument_type.
result_type Adaptable Unary Function The type of the result: AdaptableUnaryFunction1::result_type
unary_compose(const AdaptableUnaryFunction1& f,
              const AdaptableUnaryFunction2& g);
unary_compose See below.
template <class AdaptableUnaryFunction1, class AdaptableUnaryFunction2>
unary_compose<AdaptableUnaryFunction1, AdaptableUnaryFunction2> 
compose1(const AdaptableUnaryFunction1& op1, 
         const AdaptableUnaryFunction2& op2);
unary_compose See below.

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to unary_compose.
Member Description
unary_compose(const AdaptableUnaryFunction1& f,
              const AdaptableUnaryFunction2& g);
The constructor. Constructs a unary_compose object that represents the function object f o g. [1]
template <class AdaptableUnaryFunction1, class AdaptableUnaryFunction2>
unary_compose<AdaptableUnaryFunction1, AdaptableUnaryFunction2> 
compose1(const AdaptableUnaryFunction1& op1, 
         const AdaptableUnaryFunction2& op2);
Creates a unary_compose object. If f and g are, respectively, of classes AdaptableUnaryFunction1 and AdaptableUnaryFunction2, then compose1(f, g) is equivalent to unary_compose<AdaptableUnaryFunction1, AdaptableUnaryFunction2>(f, g), but is more convenient. This is a global function, not a member function.

Notes

[1] This operation is called function composition, hence the name unary_compose. It is often represented in mathematics as the operation f o g, where f o g is a function such that (f o g)(x) == f(g(x)). Function composition is a very important concept in algebra. It is also extremely important as a method of building software components out of other components, because it makes it possible to construct arbitrarily complicated function objects out of simple ones.

See also

The function object overview, binary_compose, binder1st, binder2nd.

STL Home