EDIT: It's syntactically legal, but results in undefined behavior if you use x
.
It's not legal because you're assigning an uninitialized variable with another (well, the same) uninitialized variable. Just because it compiles doesn't mean it's legal. It's valid C++ syntax, yes, but not legal.
The right hand side of the assignment operator must be fully evaluated at the time of the assignment. In this case, that's i
, which isn't initialized.
Credits to Steve Jessop, who dug up the quote:
4.1/1, lvalue-to-rvalue conversion
[...] if the object is uninitialized, a program that necessitates this
conversion has undefined behavior.