C++ Quiz168 of 173
Difficulty:
⬅️ Prev: 347Quiz ID: 348Next: 350 ➡️

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

#include <iostream>

struct Printer
{
    Printer() { std::cout << 1; }
};

template <typename T>
struct C
{
    C(T i) { std::cout << i; }
    static inline Printer printer{};
};


int main()
{
    C c(2);
}
Hint

A member of a templated class is only implicitly instantiated when needed.

Reveal answer

2

Explanation

Static members of class templates like C::printer are only instantiated if they are used in a way that requires them to be defined. C::printer is not used at all in this program, so it is never initialized, and 1 is never printed.

§[temp.inst]¶4:

Unless a member of a templated class is a declared specialization, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist or if the existence of the definition of the member affects the semantics of the program; in particular, the initialization (and any associated side effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

⬅️ Prev: 347Quiz ID: 348Next: 350 ➡️