Trying to understand where the scope exit blocks would execute with respect to automatic variables' deallocations, I wrote this program:
#include <boost/scope_exit.hpp>
#include <iostream>
class A
{
public:
A()
{
std::cout << "A's constructor" << std::endl;
}
~A()
{
std::cout << "A's destructor" << std::endl;
}
};
class B
{
public:
B()
{
std::cout << "B's constructor" << std::endl;
}
~B()
{
std::cout << "B's destructor" << std::endl;
}
};
int main()
{
A a;
BOOST_SCOPE_EXIT()
{
std::cout << "Scope exit 1" << std::endl;
} BOOST_SCOPE_EXIT_END
B b;
BOOST_SCOPE_EXIT()
{
std::cout << "Scope exit 2" << std::endl;
} BOOST_SCOPE_EXIT_END
std::cout << std::endl;
return 0;
}
The output is the following:
A's constructor
B's constructor
Scope exit 2
B's destructor
Scope exit 1
A's destructor
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?