好吧,我有点尴尬地问这个问题,但我只是想确定......
众所周知,C在布尔表达式中使用短路评估:
int c = 0;
if (c && func(c)) { /* whatever... */ }
Run Code Online (Sandbox Code Playgroud)
在该示例func(c)中未调用,因为c求值为0.但是,比较的副作用会改变下一个被比较的变量的更复杂的例子呢?像这样:
int c; /* this is not even initialized... */
if (canInitWithSomeValue(&c) && c == SOMETHING) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
函数canInitWithSomeValue返回true并在成功时更改给定指针的值.是否保证后续比较(c == SOMETHING在本例中)使用的值设置为canInitWithSomeValue(&c)?
无论编译器使用多么繁重的优化?