C++ Quiz117 of 173
Difficulty:
⬅️ Prev: 243Quiz ID: 244Next: 248 ➡️

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

#include <iostream>

template <typename T>
struct A {
    static_assert(false);
};

int main() {
    std::cout << 1;
}
Hint

Note that the template is not instantiated here.

Reveal answer

1

Explanation

Since C++23, this construct is legal and the code prints 1.

Before P2593, the §[temp.res.general]¶6.1 clause didn't contain an exception for static_asserts:

The program is ill-formed, no diagnostic required, if:

— no valid specialization, ignoring static_assert-declarations that fail, can be generated for a template or a substatement of a constexpr if statement within a template and the template is not instantiated (...)

So our program was ill-formed, no diagnostic required, and we had undefined behaviour. This was causing complications when writing useful compile-time checks prior to C++23. As a result, the behaviour was changed, and now the mere presence of such a declaration in a template definition has no effect (as opposed to actually instantiating the template), and the program is well-formed. §[dcl.pre]¶10:

In a static_assert-declaration, the constant-expression is contextually converted to bool and the converted expression shall be a constant expression (§[expr.const]). If the value of the expression when so converted is true or the expression is evaluated in the context of a template definition, the declaration has no effect.

⬅️ Prev: 243Quiz ID: 244Next: 248 ➡️