Boost logo

Boost :

From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-07-21 16:55:04


----- Original Message -----
From: "Robert Jones" <robertgbjones_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Monday, July 21, 2008 6:01 PM
Subject: [boost] [lambda] Writing pass-through 'standard' algorithms

>
> I encountered this difficulty in the context of passing lambda functors,
> but
> on reflection I don't the lambda aspect particularly matters.
>
> Consider this templated function
>
> template <typename InputIterator, typename Predicate>
> inline bool all_if( InputIterator first, InputIterator last, Predicate
> predicate )
> {
> return std :: find_if( first, last, std :: not1( predicate ) ) == last;
> }
>
> 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.

What exactly do not compiles? I have not founf ptr_fun on your function.
If predicate is a predicate functor this should compile, isn't it? The
following compiles:

template <typename InputIterator, typename Predicate>
inline bool all_if( InputIterator first, InputIterator last, Predicate
predicate )
{
    return std::find_if(first, last, std::not1( predicate ) ) == last;
}
struct IntGreaterThanThree
: public std::unary_function<int, bool>
{
    bool operator() (int x) const { return x > 3; }
};

int main() {
    std::vector<int> L;
    L.push_back(5);
    L.push_back(8);
    L.push_back(4);
    L.push_back(10);
    std::cout << "=" << all_if (L.begin(), L.end(), IntGreaterThanThree())
<< std::endl;
    return 0;
}

Vicente


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