pointer_to_unary_function, and if its argument is of type Result (*)(Arg1, Arg2) then ptr_fun creates a pointer_to_binary_function.
Definition Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Requirements on types The argument must be a pointer to a function that takes either one or two arguments. The argument type (s) and the return type of the function are arbitrary, with the restriction that the function must return a value; it may not be a void function.
Example See the examples in the discussions of pointer_to_unary_function and pointer_to_binary_function.
See also Function Objects, pointer_to_unary_function, pointer_to_binary_function, Adaptable Unary Function, Adaptable Binary Function
pointer_to_unary_function<Arg, Result>
Categories: functors, adaptors
Component type: type
Description Pointer_to_unary_function is a function object adaptor that allows a function pointer Result (*f)(Arg) to be treated as an Adaptable Unary Function. That is: if F is a pointer_to_unary_function<Arg, Result> that was initialized with an underlying function pointer f of type Result (*)(Arg) , then F(x) calls the function f(x). The difference between f and F is that pointer_to_unary_function is an Adaptable Unary Function, i.e. it defines the nested typedef s argument_type and result_type.
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<double>, 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<Arg, Result>
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. |