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 Default Arg The function object's argument type Result The function object's result type Model ofAdaptable Unary Function Type requirements
template <class Arg, class Result> pointer_to_unary_function<Arg, Result> 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 <class Arg, class Result> pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg));If f is of type Result (*)(Arg) then ptr_fun(f) is equivalent to pointer_to_unary_function<Arg,Result>(f), but more convenient. This is a global function, not a member. Notes See alsopointer_to_binary_function, ptr_fun, Adaptable Unary Function STL Main Page
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4