ptr_fun Function

Helper template functions used to convert unary and binary function pointers, respectively, into unary and binary adaptable functions.

template<class Arg, class Result> 
   pointer_to_unary_function<Arg, Result, Result (*)(Arg)> 
      ptr_fun(Result (*_pfunc)(Arg)); 
template<class Arg1, class Arg2, class Result> 
   pointer_to_binary_function<Arg1, Arg2, Result, Result (*)(Arg1, Arg2)> 
      ptr_fun(Result (*_pfunc)(Arg1, Arg2));

Parameters

  • _pfunc
    The unary or binary function pointer to be converted to an adaptable function.

Return Value

The first template function returns the unary function pointer_to_unary_function <Arg, Result>(*_pfunc).

The second template function returns binary function pointer_to_binary_function <Arg1, Arg2, Result>(*_pfunc).

Remarks

A function pointer is a function object and may be passed to any Standard Template Library algorithm that is expecting a function as a parameter, but it is not adaptable. To use it with an adaptor, such as binding a value to it or using it with a negator, it must be supplied with the nested types that make such an adaptation possible. The conversion of unary and binary function pointers by the ptr_fun helper function allows the function adaptors to work with unary and binary function pointers.

Example

// functional_ptr_fun.cpp 
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
#include <iostream>

int main( )
{
    using namespace std;
    vector <char*> v1;
    vector <char*>::iterator Iter1, RIter;

    v1.push_back ( "Open" );
    v1.push_back ( "up" );
    v1.push_back ( "the" );
    v1.push_back ( "opalescent" );
    v1.push_back ( "gates" );

    cout << "Original sequence contains: " ;
    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; ++Iter1 )
        cout << *Iter1 << " ";
    cout << endl;

    // To search the sequence for "opalescent" 
    // use a pointer_to_function conversion
    RIter = find_if( v1.begin( ), v1.end( ),
        not1 ( bind2nd (ptr_fun ( strcmp ), "opalescent" ) ) );

    if ( RIter != v1.end( ) )  
    {
        cout << "Found a match: " 
            << *RIter << endl;
    }
}

Output

Original sequence contains: Open up the opalescent gates
Found a match: opalescent

Requirements

Header: <functional>

Namespace: std

See Also

Reference

Standard Template Library