C++ Quiz12 of 173
Difficulty:
⬅️ Prev: 12Quiz ID: 13Next: 14 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

class A {
public:
  A() { std::cout << "a"; }
  ~A() { std::cout << "A"; }
};

class B {
public:
  B() { std::cout << "b"; }
  ~B() { std::cout << "B"; }
};

class C {
public:
  C() { std::cout << "c"; }
  ~C() { std::cout << "C"; }
};

A a;
int main() {
  C c;
  B b;
}
Hint

When are non-local static variables initialized? When are local variables initialized? When are they destroyed?

Reveal answer

acbBCA

Explanation

§[basic.start.dynamic]¶5 in the standard:

It is implementation-defined whether the dynamic initialization of a non-block non-inline variable with static storage duration is sequenced before the first statement of main or is deferred. If it is deferred, it strongly happens before any non-initialization odr-use of any non-inline function or non-inline variable defined in the same translation unit as the variable to be initialized.

Since A() is not constexpr, the initialization of a is dynamic. There are two possibilities:

  • a is initialized before main() is called.
  • The initialization of a is deferred, but it is initialized before any non-inline function in the same translation unit as it is used. And since main() and a are defined in the same translation unit, it is still initialized before main().

In either case, a is initialized before main() , i.e. before c or b are initialized. Then, c and b are initialized in order.

Before main() exits, b and c are destructed in the reverse order of their construction. Then, when main() returns, a is destructed as per §[basic.start.term]¶1 in the standard:

Constructed objects (§[dcl.init]) with static storage duration are destroyed (...) as part of a call to std::exit (§[support.start.term]). (...)

[Note 1: Returning from main invokes std::exit (§[basic.start.main]). — end note]

⬅️ Prev: 12Quiz ID: 13Next: 14 ➡️