When would it be undersirable for an expression to be short circuited in C++ language?
When would it be undersirable for an expression to be short circuited in C++ language
Collapse
X
-
Tags: None
-
I force my VB.NET code to use short-circuit evaluation all the time.
For example, say I have an if-statement that checks if a "widget" object is not null/nothing And widget.colour == "blue".
I don't want to access widget.colour if widget is null/nothing, so I short-circuit the if statement using the AndAlso operator in VB.NET. If I had simply used the And operator in VB.NET every part of the if-statement would have been evaluated...but in this case, if widget is null/nothing, accessing widget.colour would thrown an exception.
C++ doesn't have an AndAlso operator though.
For some reason I thought it always short-circuited?
Anyways, a situation where short-circuit evaluation would be undesirable is if you wanted to execute a method within the if-statement...but that method was never reached because the evaluation stopped before the method was executed.
-FrinnyLast edited by Frinavale; Sep 9 '10, 04:25 PM.Comment
-
I've always used && and ||
I never knew about the & and | operators.
Mind you, I've used & for concatenating strings...
It just makes sense to use short-circuit evaluation because there is no point in executing stuff that doesn't need to be executed.
-FrinnyComment
-
&& and & are significantly different operators.
& is bitwise AND can not be short circuited because it is a mathematical operation on the bits of the values involved.
&& is logical AND, there is no interaction (except by side effects) of the expressions on its left and right, that is each is evaluated independently. It just checks the boolean logic of the values of those expressions, it can be and always is short circuited.
& is not a non short circuited version of &&.
|| and | are logical and bitwise OR respectively and have similar restrictions for similar reasons.
I quite like the idea of being able to choose to short circuit or not to short circuit in VB.NET.Comment
-
Hehe thanks for the review Banfa
It's been a while since I've looked at c/c++ code and somehow forgot about the bitwise operators (I shouldn't have forgotten about them because I was tested about them much in college! Mind you it's been years...)Comment
Comment