Boost logo

Boost Users :

From: Simon Buchan (simon_at_[hidden])
Date: 2005-09-28 17:05:49


Peter Dimov wrote:
> Anakreon wrote:
>
>
>>struct event_locator: public unary_function<bool, Event*> {
>> int tId;
>> event_locator(const int tid) : tId(tid) {}
>> const bool operator() (Event* e) {
>>return e->hasTransaction()
>> && e->getTransaction().getId() == tId;
>> }
>>};
>
>
> The best (IMO) approach would be to define
>
> bool has_transaction_with_id( Event /*const?*/ * e, int id )
> {
> return e->hasTransaction() && e->getTransaction().getId() == id;
> }
>
> and then use bind( has_transaction_with_id, _1, tid ) as the predicate. You
> can also define hasTransactionWithId as a member of Event and then use
> bind( &Event::hasTransactionWithId, _1, tid ).

It may be more useful to generalise this further, depending on how often
it's used. (Also, it's considered better taste to not add members that
don't need to be members) Optionally:

// Would be safer with a overloadable transaction id type
bool operator==(Event const& e, int id)
{ // as above, but e. , not e->
}

and then use: find_if(..., *_1 == tId);

If that makes any sense for Events, and you are doing it a lot (but if
you are, do you really want a lambda function?).

>
> You can use
>
> bind( &Event::hasTransaction, _1 ) && bind( &Transaction::getId, bind(
> &Event::getTransaction, _1 ) ) == tid
>
> but I'm not sure that you'd want to.

Yes, lambda is rather weak when it comes to members, due to no fault of
the designers. Btw, the & op isn't needed anymore, bind takes function
references as well.


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