title: What does BOOST_SCOPE_EXIT do behind the scenes?
body: <p>Trying to understand where the scope exit blocks would execute with respect to automatic variables' deallocations, I wrote this program:</p>
  
  <pre><code>#include &lt;boost/scope_exit.hpp&gt;
  #include &lt;iostream&gt;
  
  class A
  {
    public:
      A()
      {
          std::cout &lt;&lt; "A's constructor" &lt;&lt; std::endl;
      }
      ~A()
      {
          std::cout &lt;&lt; "A's destructor" &lt;&lt; std::endl;
      }
  };
  
  class B
  {
    public:
      B()
      {
          std::cout &lt;&lt; "B's constructor" &lt;&lt; std::endl;
      }
      ~B()
      {
          std::cout &lt;&lt; "B's destructor" &lt;&lt; std::endl;
      }
  };
  
  int main()
  {
      A a;
      BOOST_SCOPE_EXIT()
      {
          std::cout &lt;&lt; "Scope exit 1" &lt;&lt; std::endl;
      } BOOST_SCOPE_EXIT_END
      B b;
      BOOST_SCOPE_EXIT()
      {
          std::cout &lt;&lt; "Scope exit 2" &lt;&lt; std::endl;
      } BOOST_SCOPE_EXIT_END
      std::cout &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>The output is the following:</p>
  
  <pre><code>A's constructor
  B's constructor
  
  Scope exit 2
  B's destructor
  Scope exit 1
  A's destructor
  </code></pre>
  
  <p>There is a common order among automatic variables and scope exit blocks. I am wondering how does boost achieve this? Does the BOOST_SCOPE_EXIT macro create an object on the stack whose destructor executes the exit block?</p>