It is recommended not to modify an object more than once in a single expression nor using it after modifying it in the same expression.
int i = 0;
++++i; // UB
++i = i++; // OK?
- I think that the last expression was UB before C++17 standard but now I guess it is OK because the assignment operator has become a sequence point.
So what do you think? and can you explain to me what value should i
in the last expression be ++i = i++;
?
I know it is of bad design to do so but it is just for education purpose. Thank you.
When I compile against C++17 or C++20:
g++ main.cpp -std=c++17 -o prog -Wall -pedantic
I still get the same warning:++i = i++;
This is the output from GCC:
main.cpp: In function ‘int main()’: main.cpp:12:12: warning: operation on ‘i’ may be undefined [-Wsequence-point] 12 | ++i = i++; | ~^~
.