C ++ Thermal Operator Execution Conditions

I am not sure about the execution guarantee for the terraced C / C ++ operator.
For example, if I am given an address and a boolean value indicating whether this address is suitable for reading, I can easily avoid bad reads using if / else:

int foo(const bool addressGood, const int* ptr) { if (addressGood) { return ptr[0]; } else { return 0; } } 

However, can the triple operator ( ?: Guarantee that ptr will not be available if addressGood not true?
Or can the compiler optimizer generate code that accesses ptr in any case (the program may crash), stores the value in the intermediate register and uses a conditional assignment to implement the ternary operator?

 int foo(const bool addressGood, const int* ptr) { // Not sure about ptr access conditions here. return (addressGood) ? ptr[0] : 0; } 

Thanks.

+6
source share
4 answers

Yes, the standard ensures that ptr is only available if addressGood is true. See this answer for this question, which the standard quotes:

Group conditional expressions from right to left. The first expression is contextually converted to bool (section 4). It is calculated, and if it is true, the result of the conditional expression is the value of the second expression , otherwise, the third expression. Only one of the second and third expressions is evaluated. Each calculation of the value and side effect associated with the first expression are sequenced before each calculation of the value and side effect associated with the second or third expression.

(C ++ 11 standard, paragraph 5.16 / 1)

+8
source

The conditional (ternary) operator guarantees only the evaluation of the second operand if the first operand is compared with not equal to 0 and evaluates only the third operand if the first operand is compared with equal to 0. This means that your code is safe.

After evaluating the first operand, there is also a sequence point.

By the way, you do not need parentheses - addressGood ? ptr[0] : 0 addressGood ? ptr[0] : 0 also good.

+1
source

I would say, in addition to the answer, that "yes, this is guaranteed by the C ++ standard":

Please use the first form. It is much clearer what you are trying to achieve.

I can almost guarantee that any sane compiler (with a minimal amount of optimization) will in any case generate exactly the same code for both examples.

Therefore, while it is useful to know that both of these forms achieve the same “protection,” it is definitely preferable to use the form that is most readable.

It also means that you don’t need to write a comment explaining that it is safe because of a paragraph of the same in the C ++ standard, thereby making both occupy the same amount of code space, because if you didn’t do this, know it earlier, then you can rely on someone else, not knowing that it is safe, and then spend the next half hour to find the answer via google and either run into this thread or ask a question again!

+1
source

C ++ 11 / [expr.cond] / 1

 Conditional expressions group right-to-left. The first expression is contextually converted to bool (Clause 4). It is evaluated and if it is true, the result of the conditional expression is the value of the second expression, otherwise that of the third expression. Only one of the second and third expressions is evaluated. Every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second or third expression 
0
source

Source: https://habr.com/ru/post/949504/


All Articles