pointer_to_unary_function, ptr_fun


With this object, you can use a regular C unary function as a unary function object. When executed, a pointer_to_unary_function object returns the result of executing the regular C function with an operand. Use the associated adapter function ptr_fun() to conveniently construct a pointer_to_unary_function object directly from a C unary function.

Library

Standards<ToolKit>

Declaration


#include <functional>

template< class Arg, class Result >
class pointer_to_unary_function : public unary_function< Arg, Result >

Adaptor

pointer_to_unary_function< Arg, Result > ptr_fun( Result(*f_)( Arg ) );

Interface

Constructor
explicit pointer_to_unary_function( Result( * f )( Arg ))
Constructs a unary function object with associated unary function f .
()
Result operator()( const Arg& x ) const
Returns f ( x ) .
Example <ospace/osstd/examples/ptrunf1.cpp>
#include <iostream>
#include <algorithm>
#include <functional>

int array[ 3 ] = { 1, 2, 3 };

bool
even( int n_ )
  {
  return( n_ % 2 ) == 0;
  }

void
main()
  {
  int* p = find_if
    (
    array,
    array + 3,
    pointer_to_unary_function< int, bool >( even )
    );

  if ( p != array + 3 )
    cout << *p << " is even\n";
  }

2 is even
Example <ospace/osstd/examples/ptrunf2.cpp>
#include <iostream>
#include <algorithm>
#include <functional>

int array[ 3 ] = { 1, 2, 3 };

bool
even( int n_ )
  {
  return( n_ % 2 ) == 0;
  }

void
main()
  {
  int* p = find_if( array, array + 3, ptr_fun( even ) );

  if ( p != array + 3 )
    cout << *p << " is even\n";
  }

2 is even

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.