Here's my code:
int main() {
static int test = 0;
const int anotherInt = 1;
test = anotherInt > test ? test++ : 0;
if (anotherInt > test)
test++;
else
test = 0;
return 0;
}
Here's the warning produced when I build it:
../main.cpp:15:40: warning: operation on ‘test’ may be undefined [-Wsequence-point]
test= anotherInt>test ? test++ : 0;
^
Why does C++ give me a warning on the ternary operation, but not the regular if..else
statement?
They are not equivalent. Note that in the ternary operator expression, you assigned the result to test
Change the if
condition to:
if(anotherInt > test)
test = test++; // undefined!
You would probably see the same warning here as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With