title: Should compilers ignore unused variables that result in constructors or destructors being run?
body: <p>I have some code that I put in a destructor to ensure it is run both on normal exit and exception stack unwinding:</p>
  
  <pre><code>struct withProtectedClose {
  
    ~withProtectedClose() {
      // Do some cleanup here...
    }
  };
  
  void test() {
    withProtectedClose close;
  
    // Do some work before closing
  }
  </code></pre>
  
  <p>Yet the g++ compiler (g++ (GCC) 3.4.6 20060404 (Red Hat 3.4.6-11)) is complaining:</p>
  
  <pre><code>test.cpp: In function `void test()':
  test.cpp:28: warning: unused variable 'close'
  </code></pre>
  
  <p>I can silence it by referring to the variable somehow, but that muddies the code only to silence the compiler, not how I want my code influenced.</p>
  
  <p>Shouldn't the fact that there is a destructor be enough to clue the compiler in that it makes no difference that there is no use of the variable after construction?</p>
  
  <p>Assuming the compiler cannot be shut up while still getting notices of legitimate unused variables, is there a way to silence this one only other than by using it?</p>