pointer_to_binary_function, ptr_fun


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

Library

Standards<ToolKit>

Declaration


#include <functional>

template< class Arg1, class Arg2, class Result >
class pointer_to_binary_function : public binary_function< Arg1, Arg2, Result >

Adaptor

pointer_to_binary_function< Arg1, Arg2, Result > ptr_fun
  (
  Result (*f_)( Arg1, Arg2 )
  );
  

Interface

Constructor
explicit pointer_to_binary_function( Result( * f ( Arg1, Arg2))
Constructs a binary function object with an associated function f .
()
Result operator()( const Arg1& x , const Arg2& y ) const
Returns f ( x , y ) .
Example <ospace/osstd/examples/ptrbinf1.cpp>
#include <algorithm>
#include <functional>

int input1[ 4 ] = { 7, 2, 3, 5 };
int input2[ 4 ] = { 1, 5, 5, 8 };

int
sum( int x_, int y_ )
  {
  return x_ + y_;
  }

void
main()
  {
  int output[ 4 ];
  transform
    (
    input1,
    input1 + 4,
    input2,
    output,
    pointer_to_binary_function< int, int, int >( sum )
    );

  for ( int i = 0; i < 4; ++i )
    cout << output[ i ] << "\n";
  }

8
7
8
13
Example <ospace/osstd/examples/ptrbinf2.cpp>
#include <iostream>
#include <algorithm>
#include <functional>

int input1[ 4 ] = { 7, 2, 3, 5 };
int input2[ 4 ] = { 1, 5, 5, 8 };

int
sum( int x_, int y_ )
  {
  return x_ + y_;
  }

void
main()
  {
  int output[ 4 ];

  transform( input1, input1 + 4, input2, output, ptr_fun( sum ) );

  for ( int i = 0; i < 4; ++i )
    cout << output[ i ] << "\n";
  }

8
7
8
13

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