Inspired by this question about whether the compiler can optimize away a call to a function without side effects. Suppose I have the following code:
delete[]
No. Neither should it be removed by compiler nor considered to be a side effect. Consider below:
struct A {
static int counter;
A () { counter ++; }
};
int main ()
{
A obj[2]; // counter = 2
delete [] new A[3]; // counter = 2 + 3 = 5
}
Now, if the compiler removes this as side effect then, the logic goes wrong. So, even if you are not doing anything, compiler will always assume that something useful is happening (in constructor). That's the reason why;
A(); // simple object allocation and deallocation
is not optimized away.