static_assert declaration (since C++11)

Performs compile-time assertion checking.


static_assert ( bool-constexpr , message ) (since C++11)
static_assert ( bool-constexpr ) (since C++17)


bool-constexpr -

a contextually converted constant expression of type bool.

(until C++23)

an expression contextually converted to bool where the conversion is a constant expression.

(since C++23)
message - optional (since C++17) string literal that will appear as compiler error if bool-constexpr is false, except that characters not in the basic source character set are not required to appear (until C++23).

A static assert declaration may appear at namespace and block scope (as a block declaration) and inside a class body (as a member declaration).

If bool-constexpr returns true, this declaration has no effect. Otherwise a compile-time error is issued, and the text of message, if any, is included in the diagnostic message.

message can be omitted.

(since C++17)

Built-in conversions are not allowed, except for non-narrowing integral conversions to bool.

(until C++23)


Since message has to be a string literal, it cannot contain dynamic information or even a constant expression that is not a string literal itself. In particular, it cannot contain the name of the template type argument.


#include <type_traits>
static_assert(03301 == 1729); // since C++17 the message string is optional
template <class T>
void swap(T& a, T& b) noexcept
                  "Swap requires copying");
    static_assert(std::is_nothrow_copy_constructible_v<T> &&
                  "Swap requires nothrow copy/assign");
    auto c = b;
    b = a;
    a = c;
template <class T>
struct data_structure
                  "Data structure requires default-constructible elements");
struct no_copy
    no_copy ( const no_copy& ) = delete;
    no_copy () = default;
struct no_default
    no_default () = delete;
int main()
    int a, b;
    swap(a, b);
    no_copy nc_a, nc_b;
    swap(nc_a, nc_b); // 1
    [[maybe_unused]] data_structure<int> ds_ok;
    [[maybe_unused]] data_structure<no_default> ds_error; // 2

Possible output:

1: error: static assertion failed: Swap requires copying
2: error: static assertion failed: Data structure requires default-constructible elements

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 2039 C++11 only the expression before conversion is required to be constant the conversion must also be valid in a constant expression

See also

shows the given error message and renders the program ill-formed
(preprocessing directive)
aborts the program if the user-specified condition is not true. May be disabled for release builds
(function macro)
causes abnormal program termination (without cleaning up)
causes normal program termination with cleaning up
conditionally removes a function overload or template specialization from overload resolution
(class template)
Type traits (C++11) defines a compile-time template-based interface to query or modify the properties of types
C documentation for Static assertion

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.