title: Will a destructor unit test be optimised away?
body: <p>I have created an <code>Initializer</code> class for my project which performs initialization in it's constructor and deinitialization in its destructor. The user of my project creates an <code>Initializer</code> object on the stack once at the start of his program, ensuring that deinitialization takes place when it goes out of scope (e.g. at program termination):</p>
  
  <pre><code>Class Initialiser
  {
     Initialiser()
     {
        // Acquire resources
     }
     ~Initialiser()
     {
        // Free resources
     }
  }
  </code></pre>
  
  <p>I want to create a unit test to ensure that the deinitialization takes place correctly e.g.:</p>
  
  <pre><code>bool verifyDeinitialization()
  {
     // Code to check that no resources are allocated
     // Return true if none allocated, false otherwise
  }
  
  // Syntax below is for UnitTest++:
  
  TEST(Initializer_Destructor_DeinitializationWorks)
  {
     {
        Initializer init; // Will this be optimized away?
     } // init goes out of scope, destructor called
     CHECK(verifyDeinitialization())
  }
  </code></pre>
  
  <p>My question is - if the destructor completely reverses the effects of the constructor by freeing all the resources it acquired, will the compiler detect that nothing observable has happened, and optimise away the <code>init</code> object? I'm concerned that my unit test could then provide a false negative (pass the test when it potentially could have been a fail), as correct deinitialization is equivalent to initialization never having taken place in the first place (in both cases, there are no resources which have been acquired).</p>