Topic: bindOR and bindAND


Author: "Gary Powell" <gary.powell@sierra.com>
Date: 1998/05/14
Raw View
Is there a reason the standard STL library dosen't include the binders
bindOR and bindAND?

I'd like to be able to do the following, which seems so useful I'm
wondering why it isn't part of the standard.

int main(int, char **)
{
 int const TArray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15 }; // test array.
 int const LenTArray = sizeof(TArray)/sizeof(TArray[0]);

 int *i = find_if(&TArray[0], &TArray[LenTArray],
  bindOR(
  bind2nd(greater_equal<int>(), 3),
  bind2nd(less_equal<int>(), 11)
  )
  );

 int *i2 = find_if(&TArray[0], &TArray[LenTArray],
  bindAND(
  bind2nd(greater_equal<int>(), 3),
  bind2nd(less_equal<int>(), 11)
  )
  );
}

where bindOR and bindAND are defined as follows
// Helper template for bindOR
template <class binOp1, class binOp2>
class BinderOR
: public std::unary_function<typename binOp1::argument_type, bool> {
private:
 binOp1 op1;
 binOp2 op2;
public:
 BinderOR(binOp1 const &x, binOp2 const &y)
  : op1(x), op2(y) {}

 bool operator()(typename binOp1::argument_type const & rhs) const
 { return ( op1(rhs) || op2(rhs) ); }

 // non standard extension, if you can you should use operator()(..)const.
 bool operator()(typename binOp1::argument_type const & rhs)
 { return ( op1(rhs) || op2(rhs) ); }
};

// function binder bindOR
template <class binOp1, class binOp2>
BinderOR<binOp1, binOp2> bindOR(binOp1 const &op1, binOp2 const &op2)
{ return BinderOR<binOp1, binOp2>(op1, op2); }

// Helper template for bindAND
template <class binOp1, class binOp2>
class BinderAND
: public std::unary_function<typename binOp1::argument_type, bool> {
private:
 binOp1 op1;
 binOp2 op2;
public:
 BinderAND(binOp1 const &x, binOp2 const &y)
  : op1(x), op2(y) {}

 bool operator()(typename binOp1::argument_type const & rhs) const
 { return ( op1(rhs) && op2(rhs) ); }

 // non standard extension, if you can you should use operator()(..)const.
 bool operator()(typename binOp1::argument_type const & rhs)
 { return ( op1(rhs) && op2(rhs) ); }
};

// function binder bindAND
template <class binOp1, class binOp2>
BinderAND<binOp1, binOp2> bindAND(binOp1 const &op1, binOp2 const &op2)
{ return BinderAND<binOp1, binOp2>(op1, op2); }

-gary-
gary.powell@sierra.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]