A functor from scratch that works with std bind2nd

For a functor to work with bind2nd, it usually derives from binary_function.
However, making one from scratch is not much work as illustrated in the following.

struct FunctorAdd //takes two parameters, operator() returns the sum of the two.
int operator()(const int & i, const int & j) const 
{ return i+j; 
typedef int result_type;
typedef int first_argument_type;
typedef int second_argument_type;
void Test_bind2nd()
int a[] = {0, 2, 3 };
int c = count_if ( a, a + 3, bind2nd(FunctorAdd(), 0) ); //bind2nd is a system adapter in std
//count the ones that are not 0
_ASSERT(c == 2); //this proves FunctorAdd works well with bind2nd
Comments are closed.
%d bloggers like this: