C++ Reference ยท Keywords and Statements
The static_assert declaration
Reference for C++ static_assert: checking constant conditions at compile time, custom messages, the C++17 optional message, uses with type traits and templates.
static_assert checks a condition while compiling. If the condition is false, compilation fails and the compiler prints your message. Nothing is generated: there is no runtime cost and no runtime failure, which makes it the right tool for facts that must hold for the program to be correct at all, such as "this struct is 16 bytes" or "T is an integer type".
It is a declaration, so it can appear at namespace scope, in a class body, or in a function. In templates it is evaluated when the template is instantiated, which makes it a clear way to reject bad type arguments.
static_assert(sizeof(int) >= 4, "int must be at least 32 bits"); static_assert(std::is_integral_v<T>); // message optional since C++17
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
static_assert(constant-expression, string-literal); // C++11 static_assert(constant-expression); // C++17: message optional static_assert(constant-expression, constant-string); // C++26: message may be computed
| Part | Description |
|---|---|
constant-expression |
Evaluated at compile time and contextually converted to bool |
| message | Shown in the diagnostic if the condition is false; in C++26 it may be any constant expression with size() and data() |
Errors: a false condition makes the program ill-formed, and the compiler must print the message. A non-constant condition is also an error.
Usage notes
- The condition must be a constant expression: literals,
constexprvalues,sizeof,alignof, type traits (std::is_same_v,std::is_integral_v),noexcept(...),requiresexpressions andconstexprfunction calls. - A good message states the requirement and, ideally, how to fix it: "T must be default constructible". Without a message the compiler quotes the condition text instead.
- In templates, a condition that does not depend on a template parameter may fire immediately. Writing
static_assert(false)in a template branch that is never instantiated was an error before C++23 (the condition does not depend onT); C++23 accepts it, and older code used a dependent false such asstatic_assert(always_false<T>). - Prefer
requiresor concepts to select overloads; usestatic_assertto give a readable error after selection. - Use it to pin ABI assumptions: struct sizes, enum underlying types,
std::endian::native == std::endian::little. - It is not the same as
assert:assertchecks at runtime and disappears withNDEBUG,static_assertalways runs at compile time. - Inside a function, the assertion is checked even if the function is never called (unless it is in a discarded
if constexprbranch of a template). - Added in C++11; the message became optional in C++17.