C
In C, i = i+1
and i += 1
are not equivalent if i
is an atomic type, because the compound assignment is a read-modify-write operation with memory_order_seq_cst
semantics, per C 2018 6.5.16.2 3. I also cannot say the C standard is completely clear on the semantics of i = i+1
and i += 1
in regard to volatile objects. Otherwise, ++i
, i = i+1
, and i += 1
are equivalent, given that i
is merely an identifier, not a placeholder for any more complicated expression.
C++
In C++, the operations are not equivalent. Proof:
This program:
#include <iostream>
class SensitiveToOperations
{
public:
SensitiveToOperations operator ++() { std::cout << "Preincrement.\n"; return *this; }
SensitiveToOperations operator +(int that) const { std::cout << "Addition.\n"; return *this; }
SensitiveToOperations operator =(SensitiveToOperations that) { std::cout << "Assignment.\n"; return *this; }
SensitiveToOperations operator +=(int that) { std::cout << "AdditionAssignment.\n"; return *this; }
};
int main(void)
{
SensitiveToOperations i;
++i;
i = i + 1;
i += 1;
}
produces this output:
Preincrement.
Addition.
Assignment.
AdditionAssignment.
thus showing that different results may be obtained for the different operations.
For fundamental types, the operations may be largely equivalent, but I am not qualified to speak to C++ semantics with regard to atomic or volatile.