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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tefo Seleka
    New Member
    • Sep 2010
    • 1

    #1

    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?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      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
      Last edited by Frinavale; Sep 9 '10, 04:25 PM.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        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

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          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

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            && 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

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              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

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                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

                Comment

                Working...