cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : functional : binary_function
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
functional
binary_function
unary_function
operator classes:
· divides
· equal_to
· greater
· greater_equal
· less
· less_equal
· logical_and
· logical_not
· logical_or
· minus
· modulus
· multiplies
· negate
· not_equal_to
· plus
adaptor functions:
· bind1st
· bind2nd
· mem_fun
· mem_fun_ref
· not1
· not2
· ptr_fun
types:
· binary_negate
· binder1st
· binder2nd
· const_mem_fun1_ref_t
· const_mem_fun1_t
· const_mem_fun_ref_t
· const_mem_fun_t
· mem_fun1_ref_t
· mem_fun1_t
· mem_fun_ref_t
· mem_fun_t
· pointer_to_binary_fu...
· pointer_to_unary_fun...
· unary_negate

-

binary_function class template
template <class Arg1, class Arg2, class Result> struct binary_function;
<functional>

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:

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

// 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

unary_function Unary function object base class (class template)

Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us