Type requirements
AdaptableBinaryFunction must be a model of Adaptable Binary Function. AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must both be models of Adaptable Unary Function. The argument types of AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must be convertible to each other. The result types of AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must be convertible, respectively, to the first and second argument types of AdaptableBinaryFunction .
Public base classes
unary_function
Members
Member |
Where defined |
Description |
argument_type |
Adaptable Unary Function |
The type of the function object's argument: AdaptableUnaryFunction::argument_type . |
result_type |
Adaptable Unary Function |
The type of the result: AdaptableBinaryFunction::result_type |
binary_compose(const AdaptableBinaryFunction& f, const AdaptableUnaryFunction1& g1, const AdaptableUnaryFunction1& g2); |
binary_compose |
See below. |
template binary_compose compose2(const AdaptableBinaryFunction&, const AdaptableUnaryFunction1&, const AdaptableUnaryFunction2&); |
binary_compose |
See below. |
New members
These members are not defined in the Adaptable Unary Function requirements, but are specific to binary_compose .
Member |
Description |
binary_compose(const AdaptableBinaryFunction& f, const AdaptableUnaryFunction1& g1, const AdaptableUnaryFunction1& g2); |
The constructor. Constructs a binary_compose object such that calling that object with the argument x returns f(g1(x), g2(x)) . |
template binary_compose compose2(const AdaptableBinaryFunction&, const AdaptableUnaryFunction1&, const AdaptableUnaryFunction2&); |
Creates a binary_compose object. If f , g , and g2 are, respectively, of classes AdaptableBinaryFunction , AdaptableUnaryFunction1 , and AdaptableUnaryFunction2 , then compose2(f, g1, g2) is equivalent to binary_compose(f, g1, g2) , but is more convenient. This is a global function, not a member function. |
Notes
[1] This is a form of function composition. The unary_compose adaptor allows composition of Adaptable Unary Functions; note, however, that once binary functions are introduced, there are several possible patterns of function composition. The binary_compose allows you to form a unary function by putting together two unary functions and a binary function, but you could also, for example, imagine putting together two unary functions and a binary function to form a binary function. In that case, f , g1 , and g2 would be combined into a function object h such that h(x,y) = f(g1(x), g2(y)) .
See also
The function object overview, unary_compose , binder1st , binder2nd .
Categories: functors, adaptors
Component type: type
Description
Mem_fun_t is an adaptor for member functions. If X is some class with a member function Result X::f() (that is, a member function that takes no arguments and that returns a value of type Result [1]), then a mem_fun_t is a function object adaptor that makes it possible to call f() as if it were an ordinary function instead of a member function.
Mem_fun_t 's constructor takes a pointer to one of X 's member functions. Then, like all function objects, mem_fun_t has an operator() that allows the mem_fun_t to be invoked with ordinary function call syntax. In this case, mem_fun_t 's operator() takes an argument of type X* .
If F is a mem_fun_t that was constructed to use the member function X::f , and if x is a pointer of type X* , then the expression F(x) is equivalent to the expression x->f() . The difference is simply that F can be passed to STL algorithms whose arguments must be function objects.
Mem_fun_t is one of a family of member function adaptors. These adaptors are useful if you want to combine generic programming with inheritance and polymorphism, since, in C++, polymorphism involves calling member functions through pointers or references.
As with many other adaptors, it is usually inconvenient to use mem_fun_t 's constructor directly. It is usually better to use the helper function mem_fun instead.
Example
struct B {
virtual void print() = 0;
};
struct D1 : public B {
void print() { cout << "I'm a D1" << endl; }
};
struct D2 : public B {
void print() { cout << "I'm a D2" << endl; }
};
int main() {
vector V;
V.push_back(new D1);
V.push_back(new D2);
V.push_back(new D2);
V.push_back(new D1);
for_each(V.begin(), V.end(), mem_fun(&B::print));
}
Definition
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Template parameters
Parameter |
Description |
Result |
The member function's return type. |
X |
The class whose member function the mem_fun_t invokes. |
Model of
Adaptable Unary Function
Type requirements
• X has at least one member function that takes no arguments and that returns a value of type Result . [1]
Public base classes
unary_function
Members
Member |
Where defined |
Description |
argument_type |
Adaptable Unary Function |
The type of the argument: X* |
result_type |
Adaptable Unary Function |
The type of the result: Result |
Result operator()(X* x) const |
Unary Function Function |
call operator. Invokes x->f() , where f is the member function that was passed to the constructor. |
explicit mem_fun_t(Result (X::*f)()) |
mem_fun_t |
See below. |
template mem_fun_t mem_fun(Result (X::*f)()); |
mem_fun_t |
See below. |
New members
These members are not defined in the Adaptable Unary Function requirements, but are specific to mem_fun_t .
Читать дальше