C++

C++ static_assert Failed Fix

A static_assert is a contract the compiler checks before the program exists. When it fails, the code and the contract disagree, and the diagnostic tells you which condition failed, what it reduced to, and where. This guide reads that diagnostic line by line, separates the assertion that is wrong from the assertion that is right, and fixes each case without deleting the check that caught the problem.

Written November 28, 2026 · Tested with GCC 13.3.0 · 19 min read

By the end of this guide
  • The failure: a condition that is false at compile time
  • The no-message form: what GCC prints without the string
  • Fix 1: make the condition depend on the template parameter
  • Fix 2: when the assertion is right, fix what it guards

Written November 28, 2026

Tested with GCC 13.3.0

What this guide is

static_assert performs compile-time assertion checking. Its condition must be a constant expression: something the compiler can evaluate while compiling, such as a sizeof result, a type trait, or a constexpr value. If the condition evaluates to true, the declaration has no effect and the program compiles on. If it evaluates to false, the program is ill-formed and the compiler issues an error, including the message text if one was provided.

That makes the failure in this guide different in kind from a runtime error. No executable is produced, nothing runs, and there is no input that avoids it: the condition was already false in the source. The fix is therefore always a change to the source — either the condition was written wrong, the code it guards drifted away from what the condition demands, or the assertion was placed somewhere it fires even when it should not be evaluated at all. Those three causes look identical at the first word of the diagnostic and need three different fixes, which is why reading the whole diagnostic, including its note: lines, comes before editing anything.

This guide works through the basic failure first: an assertion on a size and an assertion on a type trait, in one program, drawing two errors with two different notes. It then covers the no-message form that C++17 allows, the template trap — a static_assert(false, ...) that fires from a branch no call ever takes — and the legitimate case, where the assertion is right and the struct it guards is what has to change. A final section separates the similar errors: a non-constant condition draws a different error entirely, and a runtime assert failure is not this error at all.

Build every program in this guide with g++ -std=c++20 -g.

The failure: a condition that is false at compile time

This program asserts two things about the target it is compiled for, and both are false:

C++
#include <type_traits>

int main() {
    static_assert(sizeof(int) == 8, "int must be 8 bytes on this target");
    static_assert(std::is_same_v<int, long>, "int and long must be the same type");
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o basic basic.cpp
basic.cpp: In function 'int main()':
basic.cpp:4:31: error: static assertion failed: int must be 8 bytes on this target
    4 |     static_assert(sizeof(int) == 8, "int must be 8 bytes on this target");
      |                   ~~~~~~~~~~~~^~~~
basic.cpp:4:31: note: the comparison reduces to '(4 == 8)'
basic.cpp:5:24: error: static assertion failed: int and long must be the same type
    5 |     static_assert(std::is_same_v<int, long>, "int and long must be the same type");
      |                   ~~~~~^~~~~~~~~~~~~~~~~~~~

Read the first error in its parts. The fixed wording is static assertion failed, and everything after the colon is the message text from the source, reproduced verbatim: int must be 8 bytes on this target. The compiler does not paraphrase the message, so the fastest way to find the assertion that fired in a large build log is to search the log for a phrase from the message, then search the source for the same phrase. The caret span sits under the condition, sizeof(int) == 8 — not under the whole declaration — marking the exact expression that evaluated to false.

The note: line under the first error is the most useful line in the output: the comparison reduces to '(4 == 8)'. GCC evaluated the condition and shows the reduced values, so the disagreement is stated in numbers — sizeof(int) is 4 on this target, the assertion demanded 8. When a note of this form appears, believe it over your assumption about the target: the assertion was written for a platform or a type size that the current compilation does not have, and the remaining question is whether the code or the assertion is the one that is out of date.

The second error has no note: line. A trait such as std::is_same_v<int, long> is not a comparison GCC can reduce to numbers, so there is nothing extra to print: the caret span under std::is_same_v<int, long> and the message are the whole report. Two assertions in one file therefore produce two different shapes of output, and the presence or absence of the reduction note tells you whether the condition was an arithmetic comparison the compiler could evaluate to concrete values.

One more property of this output matters in real builds: both assertions were reported in a single compile. A failing static_assert does not stop the compiler from checking the rest of the translation unit, so a build log can contain several of these errors from one file, each with its own message. Fix them one message at a time rather than assuming the first is the only one.

The no-message form: what GCC prints without the string

Since C++17 the message is optional, and static_assert can be written with the condition alone:

C++
int main() {
    static_assert(sizeof(int) == 8);
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o nomsg nomsg.cpp
nomsg.cpp: In function 'int main()':
nomsg.cpp:2:31: error: static assertion failed
    2 |     static_assert(sizeof(int) == 8);
      |                   ~~~~~~~~~~~~^~~~
nomsg.cpp:2:31: note: the comparison reduces to '(4 == 8)'

The diagnostic is the same error with the colon and the message removed: error: static assertion failed, full stop. The source line and the caret span are printed as before, and the reduction note still appears, so in a single-file program the failing condition is easy to find. In a large build the cost of the missing message shows: there is no phrase to search for, and every no-message failure in every file produces the identical error text, distinguishable only by file and line. The no-message form is reasonable for a condition that explains itself at the line it sits on; for an assertion guarding a layout, a protocol, or a platform assumption, the message is the part of the diagnostic a reader six months later actually needs. Adding a message to an existing assertion changes nothing about when it fires — it only changes what the failure says.

The template trap: an assertion that fires from a branch never taken

Trap
The most confusing form of this error comes from a deliberate idiom gone wrong: a catch-all branch that is supposed to reject any type the earlier branches did not handle. Written with a plain if, it looks like this:
C++
#include <type_traits>

template <typename T>
void process(T value) {
    if (std::is_integral_v<T>) {
        (void)value;
    } else {
        static_assert(false, "process requires an integral type");
    }
}

int main() {
    process(42);
    return 0;
}

The only call passes an int. The else branch is never executed. The program still fails:

Terminal
$ g++ -std=c++20 -g -o trap-plainif trap-plainif.cpp
trap-plainif.cpp: In instantiation of 'void process(T) [with T = int]':
trap-plainif.cpp:13:12:   required from here
trap-plainif.cpp:8:23: error: static assertion failed: process requires an integral type
    8 |         static_assert(false, "process requires an integral type");
      |                       ^~~~~
trap-plainif.cpp:8:23: note: 'false' evaluates to false

The reason is the difference between execution and instantiation. A plain if does not remove the else branch from the program: both branches are compiled for every instantiation of the template, because which branch runs is a runtime decision. The static_assert in the else branch is therefore checked when process<int> is instantiated, and its condition is the literal false, which depends on nothing — it is false for every possible T, so it fails for the int instantiation exactly as it would for any other. The first two lines of the output give this away: In instantiation of 'void process(T) [with T = int]' and required from here, pointing at the innocent call on line 13. When a static assertion failure arrives wrapped in an instantiation trace, the call in the trace is the trigger, not the culprit — the culprit is an assertion whose condition does not depend on the template parameter it sits next to.

The same trap fires outside templates entirely. In an ordinary function, even an if constexpr discarded branch is fully checked, because there is no instantiation step at which it could be skipped:

C++
#include <type_traits>

void process(int value) {
    if constexpr (std::is_integral_v<int>) {
        (void)value;
    } else {
        static_assert(false, "process requires an integral type");
    }
}

int main() {
    process(42);
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o trap-nontemplate trap-nontemplate.cpp
trap-nontemplate.cpp: In function 'void process(int)':
trap-nontemplate.cpp:7:23: error: static assertion failed: process requires an integral type
    7 |         static_assert(false, "process requires an integral type");
      |                       ^~~~~

Here the condition is the constant true, the else branch is discarded, and the assertion still fails the compile: outside a template, a discarded branch is checked in full. if constexpr discards a branch for code generation and for return-type deduction; it is not a preprocessor conditional, and it does not exempt the discarded code from being well-formed in a non-template context.

Inside a template the picture has one modern exception, and it is worth knowing precisely because so much older advice contradicts it. With if constexpr in a template, the discarded branch is not instantiated, and current compilers accept a plain static_assert(false, ...) there:

C++
#include <type_traits>

template <typename T>
void process(T value) {
    if constexpr (std::is_integral_v<T>) {
        (void)value;
    } else {
        static_assert(false, "process requires an integral type");
    }
}

int main() {
    process(42);
    return 0;
}
Command
$ g++ -std=c++20 -g -o trap trap.cpp

No error: the else branch is discarded for T = int and never instantiated, so the assertion is never checked. This behavior was settled by a defect report applied retroactively to the earlier standards, which made an uninstantiated static_assert(false, ...) well-formed; before that change, the same code was ill-formed and the dependent-false workaround in the next section was the only portable form. The workaround is still the safer spelling when the code must build with older compilers or older language modes — but it does not rescue the plain-if form above, where no branch is ever discarded: there, the fix is to restructure to if constexpr first.

Key result
the trap is not static_assert(false) itself but where it sits. With a plain if it fires at every instantiation, template or not; with if constexpr outside a template it still fires, because the discarded branch is fully checked; with if constexpr inside a template on a current compiler it does not fire at all. Which of the three you have decides whether any fix is needed.

Fix 1: make the condition depend on the template parameter

The portable fix for the trap keeps the catch-all branch and changes only the condition, from the literal false to a variable template that is false for every type but is written in terms of T:

C++
#include <iostream>
#include <type_traits>

template <typename T>
inline constexpr bool always_false = false;

template <typename T>
void process(T value) {
    if constexpr (std::is_integral_v<T>) {
        std::cout << "integral: " << value << '\n';
    } else {
        static_assert(always_false<T>, "process requires an integral type");
    }
}

int main() {
    process(42);
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o trapfix trapfix.cpp
$ ./trapfix
integral: 42

always_false<T> is a dependent expression: its value cannot be settled when the template is defined, because it names the parameter. The compiler therefore postpones checking the assertion until instantiation — and at instantiation, the branch containing it is the discarded branch for int, so it is never instantiated and never checked. The assertion has not been weakened; it has been deferred to exactly the instantiations where the branch is real. The same spelling does not rescue the plain-if version of the trap: the condition is still dependent, so the check waits for instantiation, but with a plain if both branches are instantiated, so a dependent-false assertion in the else branch fires for every T, including the good ones. Dependent false rescues the discarded-branch case; for a plain if, restructure to if constexpr first, then apply it.

The proof that the fixed assertion still does its job is to call it with the type it exists to reject:

C++
#include <type_traits>

template <typename T>
inline constexpr bool always_false = false;

template <typename T>
void process(T value) {
    if constexpr (std::is_integral_v<T>) {
        (void)value;
    } else {
        static_assert(always_false<T>, "process requires an integral type");
    }
}

int main() {
    process(2.5);
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o trapfix-bad trapfix-bad.cpp
trapfix-bad.cpp: In instantiation of 'void process(T) [with T = double]':
trapfix-bad.cpp:16:12:   required from here
trapfix-bad.cpp:11:23: error: static assertion failed: process requires an integral type
   11 |         static_assert(always_false<T>, "process requires an integral type");
      |                       ^~~~~~~~~~~~~~~
trapfix-bad.cpp:11:23: note: 'always_false<double>' evaluates to false

For double, the else branch is the taken branch, the assertion is instantiated, and it fires with the same message — now with a note naming the dependent expression and its value, 'always_false<double>' evaluates to false. That pair of outcomes is the definition of a correct fix for this trap: silent for the types the function accepts, and the original error, unchanged, for the types it rejects. A fix that merely removes the assertion, or replaces the condition with true, passes the first half of that test and fails the second.

An alternative spelling of the same idea asserts the real condition directly instead of asserting a packaged false: static_assert(std::is_integral_v<T>, "process requires an integral type"); at the top of the function body is dependent from the start, fires only at instantiation, and states the requirement positively. Prefer it when there is one requirement; prefer the always_false<T> catch-all when the branches each handle a family of types and the else exists precisely to catch everything else.

Fix 2: when the assertion is right, fix what it guards

Not every failure is a misplaced or miswritten assertion. The other common case is drift: the assertion states a requirement the program genuinely has, and the code under it changed. This struct is transmitted as a fixed 12-byte wire packet, and the assertion says so:

C++
#include <cstdint>
#include <type_traits>

struct Packet {
    std::uint32_t id;
    std::uint32_t length;
};

static_assert(sizeof(Packet) == 12, "Packet wire layout must be 12 bytes");
static_assert(std::is_same_v<decltype(Packet::id), std::uint32_t>,
              "Packet::id must be uint32_t");

int main() {
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o rightfix-bad rightfix-bad.cpp
rightfix-bad.cpp:9:30: error: static assertion failed: Packet wire layout must be 12 bytes
    9 | static_assert(sizeof(Packet) == 12, "Packet wire layout must be 12 bytes");
      |               ~~~~~~~~~~~~~~~^~~~~
rightfix-bad.cpp:9:30: note: the comparison reduces to '(8 == 12)'

The reduction note settles the facts: the struct is 8 bytes, the requirement is 12. The struct is missing its third field — the flags word the wire format carries — and the assertion caught an incomplete edit, not a compiler quirk. The fix restores the field; the assertion does not change by a character:

C++
#include <cstdint>
#include <iostream>
#include <type_traits>

struct Packet {
    std::uint32_t id;
    std::uint32_t length;
    std::uint32_t flags;
};

static_assert(sizeof(Packet) == 12, "Packet wire layout must be 12 bytes");
static_assert(std::is_same_v<decltype(Packet::id), std::uint32_t>,
              "Packet::id must be uint32_t");

int main() {
    std::cout << "Packet size: " << sizeof(Packet) << '\n';
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o rightfix rightfix.cpp
$ ./rightfix
Packet size: 12

The temptation in this situation is to edit the assertion to match the code — change the 12 to an 8 and move on. That converts a caught defect into a silent protocol break: the peer reading 12-byte packets now misreads every field after the first packet. The rule for this class is mechanical. If the requirement in the message comes from outside the code — a wire format, a file format, a hardware register map, an API contract — the code is wrong by definition and the assertion is the specification. If the requirement was the original author's guess about their own code, the assertion and the code are both candidates, and the change to make is the one that keeps them agreeing with the external fact, whichever side that is. Deleting the assertion is the one fix that is never right for this class: it removes the only check that will catch the next drift.

The same logic applies to the trait form. An is_same assertion that fires after a type was changed from std::uint32_t to int is reporting that a signature the rest of the program relies on has moved; fix the member's type back, or change every consumer deliberately — do not relax the trait to is_integral just to silence it, unless the wider family of types is genuinely acceptable everywhere the type is used.

Similar errors that are not this one

A static_assert whose condition mentions a runtime value does not produce static assertion failed. It produces a different error, because the problem is not that the condition is false but that it cannot be evaluated at compile time at all:

C++
int check(int x) {
    static_assert(x > 0, "x must be positive");
    return x;
}

int main() {
    return check(1);
}
Terminal
$ g++ -std=c++20 -g -o nonconst nonconst.cpp
nonconst.cpp: In function 'int check(int)':
nonconst.cpp:2:21: error: non-constant condition for static assertion
    2 |     static_assert(x > 0, "x must be positive");
      |                   ~~^~~
nonconst.cpp:2:19: error: 'x' is not a constant expression
    2 |     static_assert(x > 0, "x must be positive");
      |                   ^

Two errors, neither of them the title error of this guide. The first, non-constant condition for static assertion, names the real defect: a function parameter is not a constant expression, however constant the only call site looks. The second, 'x' is not a constant expression, points at the exact subexpression that is not usable in a constant expression. The fix is not to argue with the assertion but to move the check to the layer where the value exists: make the parameter a template parameter or a constexpr argument if the value truly is known at compile time, or check it at runtime if it is not. A runtime check on a runtime value is a different tool, which leads to the second similar error.

The runtime assert macro is not a static_assert with weaker spelling. It is checked when the program runs, it can be compiled out entirely, and its failure looks like this:

C++
#include <cassert>
#include <iostream>

int main(int argc, char**) {
    assert(argc > 5 && "need more than five arguments");
    std::cout << "ran\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -g -o runtime runtime.cpp
$ ./runtime
runtime: runtime.cpp:5: int main(int, char**): Assertion `argc > 5 && "need more than five arguments"' failed.

The program compiled, an executable exists, and it aborted at runtime with the word Assertion, the source location, and the stringized condition — a report from a running process, not a diagnostic from a compiler. If the message you are reading came from a process that started, it is this, whatever the condition was; if no executable was produced, it is a static_assert. The two never substitute for each other: a property of types and sizes belongs in a static_assert, where it is checked on every build at no runtime cost, and a property of inputs and runtime state belongs in a runtime check, where the value exists.

The fixes, ranked

Fix the guarded code when the assertion states an external requirement. Ranked first because it is the case where the assertion has already done the job it exists for: it caught drift between the code and a wire format, a layout, or a type contract. The reduction note gives the actual and required values; bring the code back to the requirement and leave the assertion untouched.

Make the condition dependent when a catch-all branch fires for good types. always_false<T> (or asserting the trait directly) defers the check to instantiation, which is the moment the branch is known to be real. Verify the fix in both directions: the accepted type compiles, and a rejected type still draws the original message.

Restructure with if constexpr when a plain if keeps both branches alive. Discarding is what the catch-all idiom always intended; a plain if never discards, in a template or out of it. On current compilers, if constexpr inside a template also makes a plain static_assert(false, ...) in the discarded branch well-formed — but the dependent-false spelling remains the one that survives older compilers, so prefer it where portability matters.

Correct the condition when the assertion itself misstates the requirement. The remaining case: the target really is the one being compiled for, the type really is the intended one, and the condition was written against a wrong assumption — a size remembered from another platform, a trait applied to the wrong pair. Change the condition to state the true requirement, with a message that says why, rather than deleting it: the next drift is then still caught, against the corrected contract.

Which fix for which cause

What the diagnostic shows Cause Fix
static assertion failed, note shows reduced values The guarded size or value differs from the requirement Fix the guarded code; do not edit the number in the assertion
static assertion failed on a trait, no note The type lacks the required property Restore the type or property, or restate the trait more widely
Failure wrapped in an instantiation note, call site innocent A non-dependent assertion in a branch compiled for every instantiation Make the condition dependent with the always_false trait, or assert the trait directly
Failure in a discarded else branch, ordinary function Outside a template the discarded branch is fully checked Move the assertion into a template, make it dependent, or remove it
static assertion failed, nothing after the colon The assertion uses the no-message form (C++17) Same fixes by cause; add a message so the next failure is searchable
non-constant condition for a static assertion The condition uses a runtime value Move the check to a template or constexpr value, or check at runtime
Assertion ... failed from a running program A runtime assert, not a static_assert Debug the runtime condition; no compiler fix applies

Checklist

  • Read past the fixed wording: everything after static assertion failed: is the message from the source — search the source for that phrase to find the assertion that fired.
  • Read the note: line before editing. A reduction note (the comparison reduces to '(4 == 8)') states the actual and required values in numbers; a trait assertion has no note, and the caret span is the whole report.
  • Expect several failures per compile: one failing assertion does not stop the compiler from reporting the others in the same file.
  • If the output opens with an instantiation trace, distrust the call it points at: a non-dependent condition fires for every instantiation, and the defect is in the assertion's placement, not in the caller's type.
  • Classify the branch before fixing the trap: plain if never discards, if constexpr outside a template still fully checks the discarded branch, and if constexpr inside a template on a current compiler does not instantiate it.
  • Test every trap fix in both directions: an accepted type must compile, and a rejected type must still produce the original message.
  • When the requirement comes from outside the code — a format, a protocol, a register map — fix the code and leave the assertion alone; never tune the asserted number to match drifted code.
  • Keep the two error families apart: non-constant condition for static assertion means the value is not known at compile time, and a runtime Assertion ... failed report means the program compiled and ran — neither is fixed by the remedies in this guide.

Sources

  • cppreference, static_assert declaration — for static_assert performing compile-time assertion checking, for the message being optional since C++17, for a failing assertion making the program ill-formed with the user-provided message included in the diagnostic, and for the defect report that made an uninstantiated static_assert(false) well-formed.
  • cppreference, if statement — for the constexpr if statement discarding the branch not taken, for a discarded branch outside a template being fully checked, for a discarded branch in a template not being instantiated, and for the dependent always-false variable template as the workaround spelling.
  • cppreference, Constant expressions — for what can be evaluated at compile time, for a function parameter not being usable in a constant expression, and for the contexts that require constant expressions.
  • cppreference, assert — for the runtime assert macro checking its condition during execution and aborting when it is false, as distinct from a compile-time static_assert.

Keep Exploring