C++ › Constexpr & Compile-Time

What is static_assert?

Beginner

Answer

static_assert performs a compile-time check: if the constant boolean condition is false, compilation fails with the given message. It's used to enforce assumptions (type sizes, template constraints, configuration invariants) at build time rather than discovering them at run time.
static_assert(sizeof(int) == 4, "int must be 32-bit");
Real-world example A static_assert guards that a struct's size matches a wire protocol, failing the build if it drifts.

Common follow-ups: When does static_assert fire? | Why check at compile time?