Is the destruction of automatic objects (objects created on the stack) guaranteed to be executed not before they go out of scope?
To clarify:
#include <iostream>
class A {
public:
A() {
std::cout << "1";
}
~A() {
std::cout << "3";
}
};
void test123() {
A a;
std::cout << "2";
}
To print "2"
, a
is not required any more, so theoretically the compiler could try to optimise and destroy a
as soon as it is not needed any more.
Can I rely on the above function always printing 123
?