title: Execute some code when an object goes out of scope
body: <p>There are times where I want to be sure that some function call is executed before returning from an other function, or an integer is decremented.</p>
  
  <p>For example consider this code:</p>
  
  <pre><code>void doSomething()
  {
      try
      {
          .....
          .....
          return;
          .....
          .....
          .....
          return;
          ......
      } catch ( ... )
      }
      myvariable--;
  }
  </code></pre>
  
  <p>I want to be sure that myvariable is always decremented, no mater if there is an exception, or a return call.
  Is there any build in object in C++ for this, or I should create my own class that accepts an std::function and executes it when the object gets out of scope at the object destructor?</p>
  
  <p>For example, in C# there is the try..catch...finally block where in finally you can put any code you want to execute before returning from the function.</p>