GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM
  • C library:
  • <cassert> (assert.h)
  • <cctype> (ctype.h)
  • <cerrno> (errno.h)
  • <cfenv> (fenv.h)
  • <cfloat> (float.h)
  • <cinttypes> (inttypes.h)
  • <ciso646> (iso646.h)
  • <climits> (limits.h)
  • <clocale> (locale.h)

This view

  • Original page
  • New address

GeezerVison is showing cplusplus.com — std::binary_function

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Reference

  • C library:

    • <cassert> (assert.h)
    • <cctype> (ctype.h)
    • <cerrno> (errno.h)
    • C++11
      <cfenv> (fenv.h)
    • <cfloat> (float.h)
    • C++11
      <cinttypes> (inttypes.h)
    • <ciso646> (iso646.h)
    • <climits> (limits.h)
    • <clocale> (locale.h)
    • <cmath> (math.h)
    • <csetjmp> (setjmp.h)
    • <csignal> (signal.h)
    • <cstdarg> (stdarg.h)
    • C++11
      <cstdbool> (stdbool.h)
    • <cstddef> (stddef.h)
    • C++11
      <cstdint> (stdint.h)
    • <cstdio> (stdio.h)
    • <cstdlib> (stdlib.h)
    • <cstring> (string.h)
    • C++11
      <ctgmath> (tgmath.h)
    • <ctime> (time.h)
    • C++11
      <cuchar> (uchar.h)
    • <cwchar> (wchar.h)
    • <cwctype> (wctype.h)
  • Containers:

    • C++11
      <array>
    • <deque>
    • C++11
      <forward_list>
    • <list>
    • <map>
    • <queue>
    • <set>
    • <stack>
    • C++11
      <unordered_map>
    • C++11
      <unordered_set>
    • <vector>
  • Input/Output:

    • <fstream>
    • <iomanip>
    • <ios>
    • <iosfwd>
    • <iostream>
    • <istream>
    • <ostream>
    • <sstream>
    • <streambuf>
  • Multi-threading:

    • C++11
      <atomic>
    • C++11
      <condition_variable>
    • C++11
      <future>
    • C++11
      <mutex>
    • C++11
      <thread>
  • Other:

    • <algorithm>
    • <bitset>
    • C++11
      <chrono>
    • C++11
      <codecvt>
    • <complex>
    • <exception>
    • <functional>
    • C++11
      <initializer_list>
    • <iterator>
    • <limits>
    • <locale>
    • <memory>
    • <new>
    • <numeric>
    • C++11
      <random>
    • C++11
      <ratio>
    • C++11
      <regex>
    • <stdexcept>
    • <string>
    • C++11
      <system_error>
    • C++11
      <tuple>
    • C++11
      <type_traits>
    • C++11
      <typeindex>
    • <typeinfo>
    • <utility>
    • <valarray>

<functional>

  • functions

    • C++11
      bind
    • C++11
      cref
    • C++11
      mem_fn
    • not1
    • not2
    • C++11
      ref
  • wrapper classes

    • binary_negate
    • C++11
      function
    • C++11
      reference_wrapper
    • unary_negate
  • operator classes

    • C++11
      bit_and
    • C++11
      bit_or
    • C++11
      bit_xor
    • divides
    • equal_to
    • greater
    • greater_equal
    • less
    • less_equal
    • logical_and
    • logical_not
    • logical_or
    • minus
    • modulus
    • multiplies
    • negate
    • not_equal_to
    • plus
  • other classes

    • C++11
      bad_function_call
    • C++11
      hash
    • C++11
      is_bind_expression
    • C++11
      is_placeholder
  • namespaces

    • C++11
      placeholders
  • deprecated

    • binary_function
    • bind1st
    • bind2nd
    • binder1st
    • binder2nd
    • const_mem_fun_ref_t
    • const_mem_fun_t
    • const_mem_fun1_ref_t
    • const_mem_fun1_t
    • mem_fun
    • mem_fun_ref
    • mem_fun_ref_t
    • mem_fun_t
    • mem_fun1_ref_t
    • mem_fun1_t
    • pointer_to_binary_function
    • pointer_to_unary_function
    • ptr_fun
    • unary_function
  • Reference
  • <functional>
  • binary_function
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 typedefinitionnotes
first_argument_typeThe first template parameter (Arg1)Type of the first argument in member operator()
second_argument_typeThe second template parameter (Arg2)Type of the second argument in member operator()
return_typeThe 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)
Home page | Privacy policy
© cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s
Spotted an error? contact us
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.