class template

std::binary_function

<functional>
template <class Arg1, class Arg2, class Result> struct binary_function;
Binary function object base class
This is a base class for standard binary function objects.

Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore it can be used in templates instead of a pointer to a function.

In the case of binary function objects, this operator() member function takes two parameters.

binary_function is just a base class, from which specific binary function objects are derived. It has no operator() member defined (derived classes are expected to define this) - it simply has three public data members that are typedefs of the template parameters. It is defined as:

1
2
3
4
5
6
template <class Arg1, class Arg2, class Result>
  struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
  };


Members

first_argument_type
Alias of the first template parameter, which is the type for the first argument in member operator().
second_argument_type
Alias of the second template parameter, which is the type for the second argument in member operator().
result_type
Alias of the third template parameter, which is the return type in member operator().

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// binary_function example
#include <iostream>
#include <functional>
using namespace std;

struct Compare : public binary_function<int,int,bool> {
  bool operator() (int a, int b) {return (a==b);}
};

int main () {
  Compare Compare_object;
  Compare::first_argument_type input1;
  Compare::second_argument_type input2;
  Compare::result_type result;

  cout << "Please enter first number: ";
  cin >> input1;
  cout << "Please enter second number: ";
  cin >> input2;

  result = Compare_object (input1,input2);

  cout << "Numbers " << input1 << " and " << input2;
  if (result)
	  cout << " are equal.\n";
  else
	  cout << " are not equal.\n";

  return 0;
}


Possible output:

Please enter first number: 2
Please enter second number: 33
Numbers 2 and 33 are not equal.

See also