
On Fri, Feb 15, 2008 at 10:19 AM, dizzy
On Friday 15 February 2008 17:07:21 Michael Fawcett wrote:
On Fri, Feb 15, 2008 at 3:14 AM, Jean-Pierre Bergamin
wrote: Could there be a way to provide a function object safe_logical_and that only evaluates the second argument if the first one is true?
I've written something like that before as:
<snip code with typos>
but if you needed function arguments that weren't available to be bound at and_ construction time you'd have to do some more trickery. This would also scale well using variadic templates.
I don't understand how this code does not evaluate the second argument if the first one isn't true (the previous poster explicitely requested that). When you construct the above functor evaluation of the expression given for "f1" argument will happen which may run into UB, ie the classical "if (p && p->flag)" idiom is broken (and you seem to recognize that... no idea then what you ment with the code).
I might be showing my misunderstanding of all the intricacies at work here...
Maybe I am not understanding his intents, or maybe we are talking to
two different points? I have pasted below his original code, modified
to use something like what I posted (and also to have a result when &&
is used).
Are you saying that short-circuit evaluation doesn't happen here, i.e.
the second(t) call happens no matter what?
I thought it was understood that overloading && would have that
effect, and the OP understood that too, which is why he suggested a
safe_logical_and. That means overloading && is out of the question,
which we all seem to agree on, but I am feeling a little silly trying
to figure out why the below code is rubbish.
#include "boost/bind.hpp"
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace boost;
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;
}
template