Boost logo

Boost :

From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2008-07-21 14:06:05


On Tue, Jul 22, 2008 at 12:01 AM, Robert Jones <robertgbjones_at_[hidden]> wrote:
[snip]
>
> Obviously the intent is to enquire if all members of a range satisfy the
> predicate.
>
> Now, as is, this doesn't compile, because it needs ptr_fun() on the
> predicate, but then a couple of things come up.
>
> - ptr_fun( ) requires that it's argument is a pointer-to-function
> - I'd like my all_if( ) to permit functions or functors, including lambda
> functors
>
> How can this be done? Is it impossible to dress-up a standard
> algorithm in this way?
>

You can add an additional level of indirection which has different
implementations in cases where:

1. You have a function object derived from std::unary_function.
2. You have a function pointer passed as an argument.
3. You have a lambda/bind expression.

You'd have to do something like this:

template <class InputIterator, class Predicate>
inline bool all_if(InputIterator first, InputIterator last, Predicate
predicate) {
  // defer implementation
  typedef dispatch_on<Predicate>::type predicate_tag;
  return all_if_impl(first, last, predicate, predicate_tag());
}

template <class InputIterator, class Predicate>
inline bool all_if_impl(InputIterator first, InputIterator last,
Predicate predicate, unary_function_tag);

template <class InputIterator, class Predicate>
inline bool all_if_impl(InputIterator first, InputIterator last,
Predicate predicate, lambda_expression_tag);

...

HTH

-- 
Dean Michael C. Berris
Software Engineer, Friendster, Inc.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk