Boost logo

Boost Users :

From: Simon Buchan (simon_at_[hidden])
Date: 2005-09-23 03:51:03


Played around a bit, try this. It only defines is_in (which wraps
'std::find(rhs.begin(), rhs.end(), lhs) != rhs.end()' ),
but should be easily generalisable to any operation, say:
if(x <is_in> vec) {while(int i <for_all_in> vec) cout << i;}
(I don't know if you could do the last one, or what syntax would be
better suited, but you get the idea)


#include <vector>
#include <algorithm>

namespace boost { namespace infix {
namespace detail {
        template <typename LhsT, typename Operator>
        struct binder
        {
                explicit binder(LhsT& _lhs)
                : lhs(_lhs)
                {}

                template <typename RhsT>
                typename Operator::template sig<LhsT, RhsT>::type
                operator > (const RhsT& rhs)
                { return Operator::apply(lhs, rhs);
                }

                // SFINAE version of the above using ::result_type

                // ...
        private:
                LhsT& lhs;
        };

        // lots of other things
}

struct is_in_operator
{ template <typename ValueT, typename Cont>
        static bool
        apply(const ValueT& val, const Cont& cont)
        { return std::find
                ( cont.begin()
                , cont.end()
                , val
                ) != cont.end();
        }

        template <typename LhsT, typename RhsT>
        struct sig
        { typedef bool type;
        };
} is_in;

// Lots of other infix operators

template <typename LhsT, typename Operator>
detail::binder<LhsT, Operator>
operator < (LhsT& lhs, Operator rhs)
{ return detail::binder<LhsT, Operator>(lhs);
}

}}

int main()
{ using namespace boost::infix;
        std::vector<int> vec;
        int val = 6;
        vec.push_back(4);
        vec.push_back(7);
        vec.push_back(6);
        return val <is_in> vec;
}


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