Is modifying variable in its declaration statement well-defined?
int i = i=0;//no warning
The statement above is initialization and is well-defined since the two i
's are in the same scope.
As per basic.scope.pdecl#1
The point of declaration for a name is immediately after its complete
declarator and before its initializer (if any), except as noted below.
[ Example:
unsigned char x = 12; // Warning -Wunused-variable
{ unsigned char x = x; }
^ warning -Wuninitialized
Here the second x is initialized with its own (indeterminate) value. — end example ]
In the example, the second x
is in a different block scope, its value is then indeterminate. And will have a warning:
warning: 'x' is used uninitialized in this function [-Wuninitialized]
Given the fact that local variables with automatic storage will have indeterminate value if not initialized, I believe there's an assignment taking place which has this order.
int (i = (i = 0));
Example
int x; // indeterminate
int i = i = x = 2; // x is assigned to two, then x's value is assigned to i
cout << x << " " << i; // i = 2, x = 2
int i = i += 5
would not be well-defined. I have no idea about this though.int i = i&0;
? "Assignment" maybe is better here,but "modifying" is ok.Because what you said is modifying on the basis of it's undefined value,which is another situation of modifying.i=0
to run, theni
to be initialized to an unspecified state, theni
initialized fromi
. I'd also be concerned that writing/reading toi
prior to its first initliaization may be undefined instead of just an unspecified value. The fact you are usingint
makes me less certain.int i=i=0
rather thanint i=0
.It seems like thei
is used before its definition.