title: Order and point of calling destructor
body: <p>Lets say I have two local objects. When the function returns, is it guaranteed which one will go out of the scope first?</p>
  
  <p>For example:</p>
  
  <p>I have a class like this:</p>
  
  <pre><code>class MutexLock
  {
      /* Automatic unlocking when MutexLock leaves a scope */
      public:
        MutexLock (Mutex &amp;m)      { M.lock();   }
        ~MutexLock(Mutex &amp;m)      { M.unlock(); }
  };
  </code></pre>
  
  <p>This is a very common trick used to automatically release the mutex when going out of scope. But what if I need two mutexes in the scope?</p>
  
  <pre><code>void *func(void *arg)
  { 
      MutexLock m1;
      MutexLock m2;
  
      do_work();
  
  }  // m1 and m2 will get unlocked here. But in what order? m1 first or m2 first?
  </code></pre>
  
  <p>This really can't cause any deadlock. But there may be instances in which order of releasing the resource might be useful to the user. In that case is it important to be explicit rather than relying on destructors?</p>
  
  <p>Also, can destruction be delayed by the compiler in any case? What I mean is </p>
  
  <pre><code>func()
  
  {
  
       {
           foo f();
       } ---------&gt; Can compiler choose to not destroy f here, rather do it at the time when func() is returning. 
  }
  </code></pre>