According to the C++23 standard, what is the output of this program?
#include <cstdlib>
#include <iostream>
struct S {
char s;
S(char s): s{s} {}
~S() { std::cout << s; }
};
S a('a');
int main() {
S b('b');
std::exit(0);
}One of the objects does not get destroyed.
a
The std::exit function calls the destructors of all objects with static storage duration, but is not responsible for destroying local variables. Therefore, when std::exit(0); is called, only the destructor of a is called; b is a local variable and its destructor is thus not called.
[[noreturn]] void exit(int status);Effects:
— First, objects with thread storage duration and associated with the current thread are destroyed. Next, objects with static storage duration are destroyed and functions registered by calling
atexitare called. See §[basic.start.term] for the order of destructions and calls. (Objects with automatic storage duration are not destroyed as a result of callingexit().)