By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

When would it be undersirable for an expression to be short circuited in C++ language

P: 1
When would it be undersirable for an expression to be short circuited in C++ language?
Sep 9 '10 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
Can you explain what short-circuit evaluation actually does? From there it should be easy to think of a situation that undesirable because of it.
Sep 9 '10 #2

Frinavale
Expert Mod 5K+
P: 9,731
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.

-Frinny
Sep 9 '10 #3

Nepomuk
Expert 2.5K+
P: 3,112
C/C++ isn't always short-circuited - there are two and operators: && and &, the latter not being short-circuited. Likewise with || and | for the or operator. Personally, I rarely use & or | though.

Greetings,
Nepomuk
Sep 9 '10 #4

Frinavale
Expert Mod 5K+
P: 9,731
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.

-Frinny
Sep 9 '10 #5

Banfa
Expert Mod 5K+
P: 8,916
&& 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.
Sep 9 '10 #6

Frinavale
Expert Mod 5K+
P: 9,731
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...)
Sep 9 '10 #7

Nepomuk
Expert 2.5K+
P: 3,112
OK, my mistake then - but now I've learned something myself. ^^ I just know that I learned it like that for Java and they seemed to operate in the same way for C. Guess it was just luck.

Greetings,
Nepomuk
Sep 9 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.