3

Consider this

Class* p = NULL;
if( p != NULL && p->Method() == OK ){

  // stuff
}

On all compilers I've worked with, this is quite safe. I.e. the first part of the boolean expression will evaluate to false, and the call to Method() will thus not be attempted since evaluating the second part is redundant.

Is this because most compilers will optimize away the evaluation of the second part, or is it a dictated behavior from the C/C++ standards?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
sharkin
  • 12,162
  • 24
  • 86
  • 122
  • Just adding a quick note here, but compilers would not optimize anythin away if it wasn't allowed by the standard. A compiler's job is to produce code that behaves as if the standard had been followed. So a compiler must never optimize away code that could affect the behavior of the program. – jalf May 12 '09 at 13:33
  • 2
    Well, I've never seen a person vote to close his own question as a dup before. I think you deserve a badge for that! – Paul Tomblin May 12 '09 at 13:37
  • @Paul: Cheers, but this is also a special case since I didn't know what to search for from the start. – sharkin May 12 '09 at 14:56
  • Actually, asking a duplicate question because you didn't know the correct term to search for isn't that unusual. – Paul Tomblin May 12 '09 at 15:22
  • Adding this as a comment, if you want a NON-shortcircuiting and and or you can use `&` and `|` instead of `&&` and `||`, they do both bitwise operations and non-shortcircuiting boolean logic. – OmnipotentEntity Dec 01 '13 at 18:47

6 Answers6

4

Expression short cutting is guaranteed by the standard.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
4

This is called boolean short circuiting and is defined into many languages. Here is a wikipedia article that describes which languages have this feature.

Now that you know the correct name for the feature, there are other SO articles about it as well.

Community
  • 1
  • 1
Jack Bolding
  • 3,801
  • 3
  • 39
  • 44
4

С++ Standard 1998
Section 5.14

The && operator groups left-to-right. The operands are both implicitly converted to type bool(clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

Mykola Golubyev
  • 57,943
  • 15
  • 89
  • 102
  • Which standard? We've got C and C++ standards that guarantee that, not to mention numerous other languages, and two versions of the C standard (not that something this basic is likely to have changed in position). – David Thornley May 12 '09 at 13:34
3

I haven't seen it mentioned yet, so:

Short-circuiting is guaranteed by C++ except when the && or || operator being invoked is overloaded. But don't do that, because it's too confusing.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
2

This is a feature called short circuiting. This behavior is guaranteed by the C++ standard. I do not believe it is an optimization so to speak but it's much more of simply a language feature.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

It's not just optimization, it's useful to let you be more concise.

As your example shows, it lets you write a "safe" dereferencing statement in one line. Otherwise, you have to do something like:

if (p != null) {
    if (p.getValue() == 3) {
        // do stuff
    }
}

Seems trivial, but try coding in a language that doesn't have it (e.g. VB6) and you begin to sorely miss it.

It's in the language standards as other answers mention, but only because something like this needs to be clearly specified. That it can possibly compile down to optimized code is a side-effect; these days, a decent C or C++ compiler would compile the one-line or two-line statements equivilently

Matt
  • 2,001
  • 2
  • 17
  • 15