Boost logo

Boost Users :

From: David Hall (dlwh_at_[hidden])
Date: 2005-09-22 16:45:00


Tzu-Chien Chiu wrote:

>Any boost library can make me write the expression like
>
> int x = 3;
> if ( x in list(3, 5, 6, 7) ) {
> ...
> }
>
>
>
I don't know about the boost libraries, but I quickly sketched out
something you might be interested in, based on something I saw on one of
the news groups. All you have to do is to write a binary predicate
functor with bool operator()(int,<whatever your list type is>). It's
untested, but a quick syntax check with g++ 3.4.2 works fine.

Usage will be
if (x <in> list(3,5,6,7) ) { ... }

I hope that's more or less what you're after; I don't know if you want
that list type/function as well.

The code itself is attached. Be sure to fix the template parameter in
the unnamed namespace..

David Hall

template <class F>
struct operator_t
{
 operator_t(F f=F()): f(f) {}
 F f;
};

// Make a binary predicate functor that takes two arguments that iterates over your list...
namespace
{
operator_t<YOU FILL IN HERE> in;
}

namespace detail
{
template <class T, class F>
struct operator_t_bind
{
operator_t_bind(const T & x,F & f):lhs(x),f(f) {}
const T & lhs;
const F & f;
};
}

template <class T,class F>
detail::operator_t_bind<T,F> operator<(const T & lhs,operator_t<F> & op)
{
  return detail::operator_t_bind<T,F>(lhs,op.f);
}

template <class T, class F,class RHS>
bool operator>(const detail::operator_t_bind<T,F> & binder,const RHS & rhs)
{
 return binder.f(binder.lhs,rhs);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net