|
-
September 13th, 2010, 04:44 AM
#1
WHich one is evaluated first
Given
!=
==
&&
which are used within one conditional clause, how can I find out which one will be evaluated first ?
e.
if(c==a!=b&&c!=b)
//dosomething
Thank you
-
September 13th, 2010, 04:56 AM
#2
Re: WHich one is evaluated first
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 13th, 2010, 05:00 AM
#3
Re: WHich one is evaluated first
thank monarchy
-
September 13th, 2010, 07:57 AM
#4
Re: WHich one is evaluated first
Use parentheses to enforce the order so that no one else has to try to remember the precedence when they look at your code.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
September 13th, 2010, 09:38 AM
#5
Re: WHich one is evaluated first
Just a warning: "which one will be evaluated first" has to do with order of evaluation, which is sometimes a consequence of the grouping of subexpressions (and thus precedence), but generally the rules of precedence are specified, whereas order of evaluation is generally unspecified.
-
September 13th, 2010, 09:44 AM
#6
Re: WHich one is evaluated first
Also, be ware of short-circuit:
the (non-overloaded) operator&& and operator|| will NOT evaluate the second parameter if the first is false or true respectively. This is because it is useless. (false && x is always false, true || x is always true).
This is a very know behavior that is not only useful, but relied on.
Code:
int * p = 0;
...
if (p && p->is_valid())
...
if p->is_valid was evaluated, but p was null, the consequences would be catastrophic.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 13th, 2010, 01:13 PM
#7
Re: WHich one is evaluated first
 Originally Posted by monarch_dodra
Also, be ware of short-circuit:
the (non-overloaded) operator&& and operator|| will NOT evaluate the second parameter if the first is false or true respectively. This is because it is useless. (false && x is always false, true || x is always true).
This is a very know behavior that is not only useful, but relied on.
Code:
int * p = 0;
...
if (p && p->is_valid())
...
if p->is_valid was evaluated, but p was null, the consequences would be catastrophic.
Very true, but I'm sure you meant to declare p as some class type rather than int...
Old Unix programmers never die, they just mv to /dev/null
-
September 13th, 2010, 07:35 PM
#8
Re: WHich one is evaluated first
 Originally Posted by monarch_dodra
Also, be ware of short-circuit:
the (non-overloaded) operator&& and operator|| will NOT evaluate the second parameter if the first is false or true respectively. This is because it is useless. (false && x is always false, true || x is always true).
This is a very know behavior that is not only useful, but relied on.
Code:
int * p = 0;
...
if (p && p->is_valid())
...
if p->is_valid was evaluated, but p was null, the consequences would be catastrophic.
I thank Monachy I learn new things
-
September 14th, 2010, 01:41 AM
#9
Re: WHich one is evaluated first
 Originally Posted by HighCommander4
Very true, but I'm sure you meant to declare p as some class type rather than int... 
Lol. Nice catch sir.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 14th, 2010, 02:23 PM
#10
Re: WHich one is evaluated first
 Originally Posted by monarch_dodra
the (non-overloaded) operator&& and operator|| will NOT evaluate the second parameter if the first is false or true respectively.
Let me get this right ... that does not apply to overloaded && operators then.
Thinking about how it would be used, with two parameters (or 'this' and one parameter), the right hand parameter would have to be evaluated before the operator could be called.
I suppose you'd only ever overload it if you did not want the default && behaviour, but it still seems a shame that you cannot keep the 'lazy and' characteristics.
For smart pointers you could overload the boolean operator instead, but when would you ever want to overload && or || in real code ?
 Originally Posted by HighCommander4
Very true, but I'm sure you meant to declare p as some class type rather than int... 
Then again INT could have been anything.
-
September 14th, 2010, 02:27 PM
#11
Re: WHich one is evaluated first
 Originally Posted by Zaccheus
Let me get this right ... that does not apply to overloaded && operators then.
Scott Meyers recommends against overloading those operators for this reason.
-
September 15th, 2010, 02:23 AM
#12
Re: WHich one is evaluated first
 Originally Posted by Zaccheus
Let me get this right ... that does not apply to overloaded && operators then.
Thinking about how it would be used, with two parameters (or 'this' and one parameter), the right hand parameter would have to be evaluated before the operator could be called.
I suppose you'd only ever overload it if you did not want the default && behaviour, but it still seems a shame that you cannot keep the 'lazy and' characteristics.
For smart pointers you could overload the boolean operator instead, but when would you ever want to overload && or || in real code ?
I highly recommend against the operator bool. If you were to implement it, it means your type could also be cast implicitly to int or double.
Instead, implement the "operator void const*" and "operator!". void* can be on be implicitly cast implicitly to bool. Since the compiler is only allowed 2 implicit casts before giving up, this make your type convertible to bool, and that is it. This make sure you don't have any weird behaviors.
std::iostream does it this way.
In real code, there is no reason to overload && or ||. Even if the overloaded version kept the short-circuit behavior, it is much more practical to allow your type to be evaluated as true or false, and use the default operator.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 15th, 2010, 04:27 AM
#13
Re: WHich one is evaluated first
 Originally Posted by monarch_dodra
this make your type convertible to bool, and that is it. This make sure you don't have any weird behaviors.
Not really: weird behaviour is still possible, though less likely.
 Originally Posted by monarch_dodra
std::iostream does it this way.
This is weird, to me:
Code:
#include <iostream>
int main()
{
std::cout << std::cin;
}
The current solution for this weirdness is to implement the safe bool idiom; in the next version of C++ we can simply implement an explicit conversion function to bool.
-
September 15th, 2010, 04:56 AM
#14
Re: WHich one is evaluated first
 Originally Posted by laserlight
Not really: weird behaviour is still possible, though less likely.
This is weird, to me:
Code:
#include <iostream>
int main()
{
std::cout << std::cin;
}
The current solution for this weirdness is to implement the safe bool idiom; in the next version of C++ we can simply implement an explicit conversion function to bool.
All correct, but the problem with explicit conversion is that it is not implicit 
This would fail to compile with safe conversion. I would say the only absolutely safe way to do it is to provide a "operator bool_proxy", where bool_proxy is a type that can't do anything but be (implicitly) cast to bool.
Kind of like null_ptr_t* I guess: It's not a pointer, but it can be converted to a pointer.
*As a matter of fact, null_ptr can be implemented as a template, so I don't see why you can't do the same thing with bool.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 15th, 2010, 05:04 AM
#15
Re: WHich one is evaluated first
 Originally Posted by monarch_dodra
This would fail to compile with safe conversion.
No. If you insist otherwise, then be prepared: a standard library implementation that conforms to what is expected to be the next version of the C++ standard will cause your code snippet to fail to compile
Last edited by laserlight; September 15th, 2010 at 05:06 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|