"warning: operation ... may be undefined" for a triple operation - not if / else block

Here is 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 the warning appears when I create 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 about triple operation, but is not a regular expression if..else?

+4
source share
2 answers

They are not equivalent. Note that in the ternary operator expression, you assigned the resulttest

Change the condition ifto:

if(anotherInt > test)
    test = test++;  // undefined!

You will probably also see the same warning.

+3
source

u , : anotherInt > test? test ++: 0; ++ -, anotherInt > test? first.so , u , u - . U test ++ test + 1.

-3

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


All Articles