C++ Quiz171 of 173
Difficulty:
⬅️ Prev: 352Quiz ID: 354Next: 360 ➡️

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);
}
Hint

One of the objects does not get destroyed.

Reveal answer

a

Explanation

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.

§[support.start.term]¶9.1:

[[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 atexit are called. See §[basic.start.term] for the order of destructions and calls. (Objects with automatic storage duration are not destroyed as a result of calling exit().)

⬅️ Prev: 352Quiz ID: 354Next: 360 ➡️