C++ ternary operator execution conditions

2019-06-16 16:50发布

I am unsure about the guarantees of execution for the C / C++ ternary operator.
For instance if I am given an address and a boolean that tells if that address is good 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 a ternary operator (?:) guarantee that ptr won't be accessed unless addressGood is true?
Or could an optimizing compiler generate code that accesses ptr in any case (possibly crashing the program), stores the value in an intermediate register and use 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.

4条回答
霸刀☆藐视天下
2楼-- · 2019-06-16 17:04

Yes, the standard guarantees that ptr is only accessed if addressGood is true. See this answer on the subject, which quotes the standard:

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.

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

查看更多
Animai°情兽
3楼-- · 2019-06-16 17:13

I would say, in addition to the answer that "yes, it's guaranteed by the C++ standard":

Please use the first form. It's MUCH clearer what you are trying to achieve.

I can almost guarantee that any sane compiler (with minimal amount of optimisation) generates exactly the same code for both examples anyway.

So whilst it's useful to know that both of these forms achieve the same "protection", it is definitely preferred to use the form that is most readable.

It also means you don't need to write a comment explaining that it is safe because of paragraph such and such in the C++ standard, thus making both take up the same amount of code-space - because if you didn't know it before, then you can rely on someone else ALSO not knowing that this is safe, and then spending the next half hour finding the answer via google, and either running into this thread, or asking the question again!

查看更多
来,给爷笑一个
4楼-- · 2019-06-16 17:21

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.

查看更多
不美不萌又怎样
5楼-- · 2019-06-16 17:29

The conditional (ternary) operator guarantees to only evaluate the second operand if the first operand compares unequal to 0, and only evaluate the third operand if the first operand compares equal to 0. This means that your code is safe.

There is also a sequence point after the evaluation of the first operand.

By the way, you don't need the parantheses - addressGood ? ptr[0] : 0 is fine too.

查看更多
登录 后发表回答