Combining Predicates

后端 未结 3 1564
夕颜
夕颜 2021-01-05 09:08

Is there any way that you can combine predicates?

Lets say I have something like this:

class MatchBeginning : public binary_function

        
3条回答
  •  没有蜡笔的小新
    2021-01-05 09:47

    I can recommend boost.lambda for combining function-objects for such tasks. Although it is a bit heavyweight for such a simple problem. (edit) See the community wiki answer started by xhantt for a good example using STL.

    (old, deprecated, answer) You can write your own utility for this, similar:

    // here we define the combiner...
    template
    class lazy_or_impl {
      Left m_left;
      Right m_right;
    public:
      lazy_or_impl(Left const& left, Right const& right) : m_left(left), m_right(right) {}
      typename Left::result_type operator()(typename Left::argument_type const& a) const {
        return m_left(a) || m_right(a);
      }
    };
    
    // and a helper function which deduces the template arguments
    // (thx to xtofl to point this out)
    template
    lazy_or_impl lazy_or(Left const& left, Right const& right) {
      return lazy_or_impl(left, right);
    }
    

    and then use it: ... lazy_or(bind1st(...), bind1st(...)) ...

提交回复
热议问题