class template
<functional>
std::binary_function
template <class Arg1, class Arg2, class Result> struct binary_function;
template <class Arg1, class Arg2, class Result> struct binary_function; // deprecated
Binary function object base class
Note: This class has been deprecated in C++11.
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 its type can be used as template parameter when a generic function type is expected.
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 (which derived classes are expected to define) - 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
member type | definition | notes |
first_argument_type | The first template parameter (Arg1) | Type of the first argument in member operator() |
second_argument_type | The second template parameter (Arg2) | Type of the second argument in member operator() |
return_type | The third template parameter (Result) | Type returned by 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
|
// binary_function example
#include <iostream> // std::cout, std::cin
#include <functional> // std::binary_function
struct Compare : public std::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;
std::cout << "Please enter first number: ";
std::cin >> input1;
std::cout << "Please enter second number: ";
std::cin >> input2;
result = Compare_object (input1,input2);
std::cout << "Numbers " << input1 << " and " << input2;
if (result)
std::cout << " are equal.\n";
else
std::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
- unary_function
- Unary function object base class (class template
)