Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
abs
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

binary_function Structure 

Illustrates how to use the binary_function structure in the Standard Template Library (STL) in Visual C++.


template<class _A1, class _A2, class _R>
   struct binary_function
   {
      typedef _A1 first_argument_type;
      typedef _A2 second_argument_type;
      typedef _R result_type;
   };

NoteNote

The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

The binary_function<A,B,C> class is used as a base class to allow the user to easily define binary operator functions that take data types A and B as arguments and return data type C objects.

// binfunc.cpp
// compile with: /EHsc
//
// Structure used: binary_function<A,B,C> - base
//                 class used to create operator
//                 functions taking data types A
//                 and B and returning data type C.

#include <functional>
#include <iostream>

using namespace std ;

class binary_test : public binary_function<binary_test &,int,float>
{
public:
  float value;
  binary_test(){value=10.0;}
  binary_test(float x){value=x;}
  result_type operator<<(second_argument_type arg2);
};

binary_test::result_type
binary_test::operator<<(binary_test::second_argument_type arg2)
{
  value = (float)(((int)value) << arg2);
  cout << "New value after shift is " << value << endl;
  return value;
}

int main(void)
{
  binary_test item;

  cout << "Begin" << endl;
  item = item << 2;
}

Begin
New value after shift is 40

Header: < functional >

Community Additions

ADD
Show:
© 2015 Microsoft