Is compiler allowed to optimize an overloaded postfix operator and replace it with prefix operator? (provided that compiler knows what overloaded function does)
For example, in the following code, most compilers treat i++
as ++i
and generate same assembly.
for(int i=0; i<5; i++)
printf("*");
Then, could same apply for the following code?
class Integer {
int data;
Integer& operator++() { ++data; return *this; }
Integer operator++(int) { Integer ret = *(this); ++(*this); return ret; }
// And more overloads...
};
for(Integer i=0; i<5; i++)
printf("*");