static_assert
static_assert(condition, "message") is a compile-time check. If condition is false, the program does not compile and the message is printed. If it is true, the line generates no code.
Detailed Explanation
assert from <cassert> runs when the program runs. By then a wrong assumption is a crash on one machine. static_assert (C++11) runs while compiling. A wrong assumption is a build failure on every machine. That is the right place for facts that are already known to the compiler: sizes, type properties, template requirements.
The condition must be a constant expression that converts to bool. sizeof(T) == 8, std::is_trivially_copyable_v<T>, N > 0 when N is a template parameter, noexcept(T(std::declval<T>())) — those are legal. argc > 0 is not: argc is not a constant.
C++17 made the message optional: static_assert(sizeof(void*) == 8);. A message is still worth writing when the condition is not self-explanatory. The message is a string literal, not a formatted stream. C++26 adds a richer form; until then, keep the literal short and put the detail in a comment if needed.
Where it pays off:
- Template preconditions. A
Serializer<T>that only works for trivially copyableTshouldstatic_assertthat, with a sentence that says so. The error then appears at theSerializer<std::string>line, not a hundred lines down in a memcpy. - Portable layout. Code that talks to a wire format can
static_assert(sizeof(Header) == 16)andstatic_assert(alignof(Header) == 4)so a packing change fails the build instead of corrupting packets. - Feature assumptions.
static_assert(__cplusplus >= 202002L)is a blunt tool; prefer feature-test macros (__cpp_lib_jthread) when you can.
static_assert is not a substitute for concepts. A concept participates in overload resolution and gives a structured error. A static_assert inside a template fires after the template has already been selected. Use a concept (or SFINAE) to remove a candidate; use static_assert to explain a requirement that has no alternative overload.
It is also not a substitute for runtime checks. User input, file contents, and network data are not constant expressions. Validate those with if, exceptions, or std::expected.
Failed static_asserts inside unused templates do not fire. The assertion is checked when the template is instantiated. A static_assert at namespace scope is checked as soon as the compiler sees it.
static_assert(false) in a template is a special headache: some compilers diagnose it even when the template is not instantiated, because false does not depend on a template parameter. Make the condition dependent (static_assert(sizeof(T) == 0, "must handle this type")) if you are writing an always-wrong primary template that only specialisations should use.
Code Examples
#include <cstdint>
#include <cstring>
#include <type_traits>
struct Packet {
std::uint32_t id;
std::uint32_t size;
};
static_assert(sizeof(Packet) == 8, "Packet is two uint32 fields, no padding");
static_assert(std::is_trivially_copyable_v<Packet>);
template <typename T>
T loadAs(const void* bytes) {
static_assert(std::is_trivially_copyable_v<T>,
"loadAs memcpy-s the bytes; T must be trivially copyable");
T out{};
std::memcpy(&out, bytes, sizeof(T));
return out;
}#include <array>
#include <cstddef>
template <std::size_t N>
struct SmallBuf {
static_assert(N > 0, "SmallBuf cannot be empty");
std::array<char, N> data{};
};
int main() {
SmallBuf<16> buf;
(void)buf;
// SmallBuf<0> bad; // static_assert fires
}Common Gotchas
The condition must be a constant expression
`static_assert(v.size() > 0)` does not compile. Use a runtime check.
A non-dependent `static_assert(false)` can fire in an uninstantiated template
depend on `T` if the primary template is only a trap for missing specialisations.
No generated code when the assert passes
you can put them in headers and in hot templates without a runtime cost.
Messages cannot be formatted
you cannot stream `sizeof(T)` into the message in C++17/20. Put the expected value in the condition (`sizeof(T) == 16`) so the compiler prints the actual size in the error.