Boost logo

Boost Users :

From: Jean-Pierre Bergamin (james_at_[hidden])
Date: 2008-02-14 14:12:23


Hello everyone

Is there a reason why boost::bind does not overload the logical operators ||
and &&? They definitively would come in more handy to build up complicated
logical expressions than using the std::logical_and and std::logical_or
function objects.

Adding...

BOOST_BIND_OPERATOR( ||, logical_or )
BOOST_BIND_OPERATOR( &&, logical_and )

...to the operator definitions in bind.hpp works as expected with the code
below (tested with MSVC 2008).

So - why not? :-)

Regards

James

//////////////////////////////////////////////////
// Code example:
//////////////////////////////////////////////////

struct employee {
        employee(int id, const string &name) :
                id(id), name(name) {}

        int id;
        string name;
};

ostream &operator<<(ostream &o, const employee &e) {
        o << e.id << " - " << e.name << endl;
        return o;
}

typedef vector<employee> emp_container;
emp_container staff;
staff.reserve(4);

staff.push_back(employee(1, "John"));
staff.push_back(employee(2, "Paula"));
staff.push_back(employee(3, "Sara"));
staff.push_back(employee(4, "Geroge"));

emp_container::const_iterator e = find_if(staff.begin(), staff.end(),
        bind(&employee::name, _1) == "Paula" ||
        bind(&employee::name, _1) == "John" ||
        bind(&employee::id, _1) >= 4
        
);

if (e != staff.end()) {
        cout << "Found: " << *e << endl;
}


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