C ++ optimizer for deleting object with side effects

This is not currently a problem, but I am concerned that the code is ported or that we will change compilers.

I have a code with a block

{ 
   MyClass myObj;
   // copy some other variables but never touch myObj
   .
   .
} // expect destructor to be called on myObj

      

where myObj is never used in block code, but the constructor has a side effect and I rely on the MyClass destructor code to be executed at the end of the block. This works as expected in my current hand compiler with some optimization.

My question is if there is something I need to do, like declare something volatile or set some general attribute to prevent optimization from myObj as an unused variable or some such.

This is not a C ++ 11 compiler. As I said, this is not a problem, but I don't want to leave a strange future error for anyone else.

+3


source to share


2 answers


Apart from explicitly defined cases such as RVO (Return Value Optimization ) , optimization does not allow the observed behavior of the program to be changed. Optimization should be done with the so-called "as-if" rule .



+3


source


Since the compiler you are using is even marginally standard compliant (I'm looking at you, Turbo C ++). This is not a problem, because the standard gives serious guarantees regarding construction and destruction. These guarantees are the foundation of RAII , which is the foundation of the "Modern" C ++ style.



+2


source







All Articles