What is static_assert?
Short 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.
Advertisement
AModel 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.
Likely follow-ups
- When does static_assert fire? | Why check at compile time?
Related topics