Note that a function pointer of type Result (*)(Arg) is a perfectly good Unary Function object, and may be passed to an STL algorithm that expects an argument that is a Unary Function. The only reason for using the pointer_to_unary_function object is if you need to use an ordinary function in a context that requires an Adaptable Unary Function, e.g. as the argument of a function object adaptor.
Most of the time, you need not declare an object of type pointer_to_unary_function directly. It is almost always easier to construct one using the ptr_fun function.
Example
The following code fragment replaces all of the numbers in a range with their absolute values, using the standard library function fabs . There is no need to use a pointer_to_unary_function adaptor in this case.
transform(first, last, first, fabs);
The following code fragment replaces all of the numbers in a range with the negative of their absolute values. In this case we are composing fabs and negate . This requires that fabs be treated as an adaptable unary function, so we do need to use a pointer_to_unary_function adaptor.
transform(first, last, first, compose1(negate, ptr_fun(fabs)));
Definition
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Template parameters
Parameter |
Description |
Arg |
The function object's argument type |
Result |
The function object's result type |
Model of
Adaptable Unary Function
Type requirements
• Arg is Assignable.
• Result is Assignable.
Public base classes
unary_function
Members
Member |
Where defined |
Description |
argument_type |
Adaptable Unary Function |
The type of the function object's argument: Arg . |
result_type |
Adaptable Unary Function |
The type of the result: Result |
result_type operator()(argument_type x) |
Unary Function |
Function call operator. |
pointer_to_unary_function(Result (*f)(Arg)) |
pointer_to_unary_function |
See below. |
pointer_to_unary_function() |
pointer_to_unary_function |
See below. |
template pointer_to_unary_function ptr_fun(Result (*x)(Arg)); |
pointer_to_unary_function |
See below. |
New members
These members are not defined in the Adaptable Unary Function requirements, but are specific to pointer_to_unary_function .
Member |
Description |
pointer_to_unary_function(Result (*f)(Arg)) |
The constructor. Creates a pointer_to_unary_function whose underlying function is f . |
pointer_to_unary_function() |
The default constructor. This creates a pointer_to_unary_function that does not have an underlying C function, and that therefore cannot actually be called. |
template pointer_to_unary_function ptr_fun(Result (*x)(Arg)); |
If f is of type Result (*)(Arg) then ptr_fun(f) is equivalent to pointer_to_unary_function(f) , but more convenient. This is a global function, not a member. |
See also
pointer_to_binary_function , ptr_fun , Adaptable Unary Function
pointer_to_binary_function
Categories: functors, adaptors
Component type: type
Description
Pointer_to_binary_function is a function object adaptor that allows a function pointer Result (*f)(Arg1, Arg2) to be treated as an Adaptable Binary Function . That is: if F is a pointer_to_binary_function that was initialized with an underlying function pointer f of type Result (*)(Arg1, Arg2) , then F(x, y) calls the function f(x, y) . The difference between f and F is that pointer_to_binary_function is an Adaptable Binary Function, i.e. it defines the nested typedef s first_argument_type , second_argument_type , and result_type .
Note that a function pointer of type Result (*)(Arg1, Arg2) is a perfectly good Binary Function object, and may be passed to an STL algorithm that expects an argument that is a Binary Function . The only reason for using the pointer_to_binary_function class is if you need to use an ordinary function in a context that requires an Adaptable Binary Function, e.g. as the argument of a function object adaptor.
Most of the time, you need not declare an object of type pointer_to_binary_function directly. It is almost always easier to construct one using the ptr_fun function.
Example
The following code fragment finds the first string in a list that is equal to "OK" . It uses the standard library function strcmp as an argument to a function object adaptor, so it must first use a pointer_to_binary_function adaptor to give strcmp the Adaptable Binary Function interface.
list L;
…
list::iterator item = find_if(L.begin(), L.end(), not1(binder2nd(ptr_fun(strcmp), "OK")));
Definition
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Template parameters
Parameter |
Description |
Arg1 |
The function object's first argument type |
Arg2 |
The function object's second argument type |
Result |
The function object's result type |
Model of
Adaptable Binary Function
Type requirements
Arg1 is Assignable.
Arg2 is Assignable.
Result is Assignable.
Public base classes
binary_function
Members
Member |
Where defined |
Description |
first_argument_type |
Adaptable Binary Function |
The type of the first argument: Arg1 . |
second_argument_type |
Adaptable Binary Function |
The type of the second argument: Arg2 |
result_type |
Adaptable Binary Function |
The type of the result: Result |
Result operator()(Arg1 x, Arg2 y) |
Binary Function |
Function call operator. |
pointer_to_binary_function(Result (*f)(Arg1, Arg2)) |
pointer_to_binary_function |
See below. |
pointer_to_binary_function() |
pointer_to_binary_function |
See below. |
template pointer_to_unary_function ptr_fun(Result (*x)(Arg1, Arg2)); |
pointer_to_binary_function |
See below. |
New members
These members are not defined in the Adaptable Binary Function requirements, but are specific to pointer_to_binary_function .
Читать дальше