static_assert checks a Boolean expression during compilation. If the condition is false, compilation produces a diagnostic instead of allowing the program to proceed. section 8.1.5
Checking a platform assumption
static_assert(sizeof(double) == sizeof(long double), "Extra precision needed for convergence."); This assertion records a required size relationship. The condition is evaluated after preprocessing, during a later compilation phase. section 8.1.5
Keep these distinctions in mind:
- Use a preprocessor conditional for conditions the preprocessor can evaluate; use static_assert for more sophisticated compile-time conditions known only during compilation. section 8.1.5
- Write _Static_assert before C23; static_assert was provided as a macro by <assert.h> before becoming the keyword spelling in C23. section 8.1.5
- Do not confuse static_assert with assert: assert checks a Boolean expression while the program runs, whereas static_assert checks during compilation. section 8.8
static_assert(sizeof(double) == sizeof(long double), "Extra precision needed for convergence.");
Inspect the compact assertion that checks platform type-size assumptions. section 8.1.5
