I noticed that the tutorial section on operators http://www.cplusplus.com/doc/tutorial/operators/ does not mention that the && || ?: or and operators only evaluate what is necessary to calculate their results. This can be important when you see code written by a person who came from Perl like me: init_all_my_stuff() or my_abort("Couldn't load!");
That said, such code is aberrant and should not appear anywhere. It is just as efficient, if not more efficient, to use the language constructs properly: if (!init_all_my_stuff()) my_abort("Couldn't load!");
This is more easily understood by humans and the compiler is more likely to optimize it.
... Yes ... I got that much (and knew it already). As this is a C++ (and C) forum, my response is crafted for the C and C++ point of view. PHP is a disorganized mess and Perl has hysterical raisins.
"This is more easily understood by humans"
I don't agree with that. Many times we see and and or used in this capacity in English, like: Give me back my daughter, or I'll hunt you down!
"and the compiler is more likely to optimize it."
Bah... C++ relies on the compiler to optimize much harder stuff than this. I have confirmed that my compiler emits identical code for:
1 2
if(i>5)std::cout << "WIN!\n";
i>5 and std::cout << "WIN!\n";
Anyway, another reason to know this is that it can lead to accidental conditionals, like: if(flag && non_pure_function())
non_pure_function will only be called if flag is true.
Another corollary to this is that a?b:c; is the same as a&&b||c; except for precedence.
What logical fallacy? This form of conditional is found in colloquial English. Therefore it is not hard for humans to understand. The compiler knows the behavior of the standard operators when applied to POD. Therefore it can (and should) optimize that behavior. How are either of those statements fallacious?
The operators && || act as conditionals not as an optimization but as required, standard behaviour. Therefore I think it's good to use this form because it is simple, always works, and is readable to anyone with knowledge of English and its common constructions.
Also please tell me what "hysterical raisins" are.
Edit: ok, googled it. You're actually wrong, by the way, this use is encouraged in Perl for the reasons I outlined above, and because Perl likes to consider itself closer to natural language than other curly bracket languages. But that's not relevant to this discussion of C/C++. I don't understand why you don't want to use a feature of this language in a new way.
I agree with both of those, although I think that's a good reason not to overload those operators. If you did, templated code could fail mysteriously when you apply it to bool as opposed to SmartBool or ThreadSafeFlagBit. Better to overload & | instead, and use a special case for bool.
IMHO any tool should be used as intended.
This doesn't mean that short-circuit evaluation shouldn't be considered, but && and || should only be used when evaluating conditions.
Overloading them for classes representing such conditions may be useful ( maybe overloading operator bool will cause less trouble ).
Writing something like i < 10 || cout << "i >= 10"; is a bad idea and very unreadable
PHPers say the same on C++, and they are probably right. Languages or systems designed by a bunch of kids or a committee (a bunch of grownup kids) are almost never elegant, consistent and organized. I doubt the number of annoyances and sharp edges in C++ is lower than in the newest versions of PHP. At least || and && operators have consistent semantics there.
It's not xor, it's a conditional. If the unseen enemy doesn't do as I say, then the hunting will occur. If he gives back my daughter, then no hunting will occur. The hunting is only executed if my daughter is not returned.
They also differ in return value and type (this ain't python). If you aren't catching the returned value, use an if-else structure
The return value is different, but that's not relevant. There is a semicolon on the end of those sections. Besides, my point was to do with control flow. Specifically that b is only evaluated if a is not 0, and c is only evaluated if a is 0. The fact that control flow is altered by these operators can lead to mysterious bugs. Interestingly, my copy of "The C++ Programming Language" neglects to mention just what is returned by these operators. I'm writing a test program.
OK done, it always returns either one or zero.
void foo();
void bar();
(a)? foo(): bar(); //fine
a and foo() or bar(); //compile error
a?b:c; is the same as a&&b||c;
I missed it before but,
for the Boolean operators if a=true and b=false, c will be executed.
That is not the case with operator ?: that only executes a and b