class template
<functional>

std::pointer_to_binary_function

template <class Arg1, class Arg2, class Result> class pointer_to_binary_function;
Generate binary function object class from pointer
Generates a binary function object class from a pointer to a function that takes two arguments (of type Arg1 and Arg2) and returns a value (of type Result).

pointer_to_binary_function is generally used as a type. The function ptr_fun (also defined in header <functional>) can be used to directly construct an object of this type.

This class is derived from binary_function and is typically defined as:

1
2
3
4
5
6
7
8
9
10
template <class Arg1, class Arg2, class Result>
  class pointer_to_binary_function : public binary_function <Arg1,Arg2,Result>
{
protected:
  Result(*pfunc)(Arg1,Arg2);
public:
  explicit pointer_to_binary_function ( Result (*f)(Arg1,Arg2) ) : pfunc (f) {}
  Result operator() (Arg1 x, Arg2 y) const
    { return pfunc(x,y); }
};


Members

constructor
Constructs a binary function object class from pointer to a function that takes two arguments of types Arg1 and Arg2, and returns a value of type Result.
operator()
Member function taking two parameters, passing them to the function pointed by the pointer used at construction, and forwarding the return value obtained as its own return value.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// pointer_to_binary_function example
#include <iostream>
#include <functional>
#include <algorithm>
#include <cmath>
using namespace std;

int main () {
  pointer_to_binary_function <double,double,double> PowObject (pow);
  double numbers[] = {1.0, 2.0, 3.0, 4.0, 5.0};
  double squares[5];
  transform (numbers, numbers+5, squares, bind2nd(PowObject,2) );
  for (int i=0; i<5; i++)
    cout << squares[i] << " ";
  cout << endl;
  return 0;
}


Possible output:

1 4 9 16 25

See also