Here's a simple code
void GlobalAlterA(A *a) { a->m = 1; }
struct A { int n, int m; };
struct Z: public A { void AlterA() { GlobalAlterA(this); } };
struct Y: public Z {};
struct X: public Y
{
int AsValue()
{
switch (n)
{
case 0: return m;
default: AlterA(); return m;
}
}
};
Now, the problem with this code is that gcc optimizer throws away all AsValue() code except "return m", because, I suppose, it does not see the GlobalAlterA() body and doesn't know that AlterA() can change the "m" member.
Is there a way to fix this except switching off optimization for specific parts of code? E.g. tricks like volatile specifiers, etc.