0

Taking the code below as an example:

if ((n >= vector.size()) || (vector[n] == 0))

Will it raise an error if n == vector.size()?

If the answer is NO, is the || an ordered operator? Therefore, the statements beside || will be executed from left to right?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Ink
  • 845
  • 1
  • 13
  • 31
  • If you want to know if it will raise an error, you could have tested it in less time than it took to post the question. – sweenish Oct 25 '21 at 03:24
  • 1
    I believe the edit was unnecessary. – sweenish Oct 25 '21 at 03:26
  • 1
    `or` is an [alternative](https://en.cppreference.com/w/cpp/keyword/or) to `||` and has the same meaning. The latter is [short-circuiting](https://en.cppreference.com/w/cpp/language/operator_logical): If the first argument is true, it doesn't evaluate the second argument. However in this case that doesn't help, because when `n == vector.size()` `(n < vector.size())` is _false_, so it has to try to evaluate `vector[n] == 0` anyway and encounters undefined behavior. – Nathan Pierson Oct 25 '21 at 03:27
  • 1
    (1) The term for this is "short-circuiting". See the linked questions for details. (2) The standard spelling of the boolean operators are `&&` and `||` . `and` and `or` are rarely used alternate spellings that people don't normally use. I edited your question to use the common form. (3) The check needs to be `n >= vector.size()` rather than `n < vector.size()`. Or perhaps `if (n < vector.size() && vector[n] == 0)` is what you meant? – John Kugelman Oct 25 '21 at 03:33
  • 2
    @sweenish the problem with "just test it and see" is that some behaviors are unspecified or implementation-specific. (Not this one, fortunately, but if OP had asked the same question about the `|` operator, he would have gotten a different answer if he had tested under MSVC than if he had tested under g++, and neither answer would be "correct" since the "correct" answer for `|` would be "it's unspecified, so you can't rely on ordering") – Jeremy Friesner Oct 25 '21 at 03:36
  • `vector[n]` does not raise an error when n is past the final position, rather it is **undefined behavior**. You can use `vector.at(n)`, if you want the *raise an* `std::out_of_range` *error* behavior when n ≥ vector.size(). – Eljay Oct 25 '21 at 11:02
  • @JeremyFriesner I see no problem. What I read you saying is that OP asked the wrong question. They specifically asked if it would raise an error. That's a pretty cut and dry 'find out yourself' type of question. The better question would have been something along the lines of "I tried this thing out and it didn't throw an error, but is it well behaved?" We immediately have a better question because OP made an effort. – sweenish Oct 25 '21 at 12:40
  • The parentheses around the two terms aren't needed. `if (n >= vector.size() || vector[n] == 0)` means exactly the same thing as the code in the question, and doesn't require the reader to figure out that the extra parentheses aren't doing anything. – Pete Becker Oct 25 '21 at 13:38

0 Answers0