static_assert declaration (since C++11) - cppreference.com (original) (raw)

Performs compile-time assertion checking.

Contents

[edit] Syntax

static_assert( bool-constexpr , unevaluated-string ) (1)
static_assert( bool-constexpr ) (2) (since C++17)
static_assert( bool-constexpr , constant-expression ) (3) (since C++26)

Declares a static assertion. If the assertion fails, the program is ill-formed, and a diagnostic error message may be generated.

  1. A static assertion with fixed error message.

  2. A static assertion without error message.

  3. A static assertion with user-generated error message.

This syntax can only be matched if syntax (1) does not match.

[edit] Explanation

bool-constexpr -
unevaluated-string - an unevaluated string literal that will appear as the error message
constant-expression - a constant expression msg satisfying all following conditions: msg.size() is implicitly convertible to std::size_t. msg.data() is implicitly convertible to const char*.

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 is well-formed and evaluates to true, or is evaluated in the context of a template definition and the template is uninstantiated, this declaration has no effect. Otherwise a compile-time error is issued, and the user-provided message, if any, is included in the diagnostic message.

The text of the user-provided message is determined as follows:

[edit] Notes

The standard does not require a compiler to print the verbatim text of error message, though compilers generally do so as much as possible.

Since the error 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. (until C++26)
Feature-test macro Value Std Feature
__cpp_static_assert 200410L (C++11) static_assert (syntax (1))
201411L (C++17) Single-argument static_assert (syntax (2))
202306L (C++26) User-generated error messages (syntax (3))

[edit] Keywords

static_assert

[edit] Example

#include #include   static_assert(03301 == 1729); // since C++17 the message string is optional   template void swap(T& a, T& b) noexcept { static_assert(std::is_copy_constructible_v, "Swap requires copying"); static_assert(std::is_nothrow_copy_constructible_v && std::is_nothrow_copy_assignable_v, "Swap requires nothrow copy/assign"); auto c = b; b = a; a = c; }   template struct data_structure { static_assert(std::is_default_constructible_v, "Data structure requires default-constructible elements"); };   template constexpr bool dependent_false = false; // workaround before CWG2518/P2593R1   template struct bad_type { static_assert(dependent_false, "error on instantiation, workaround"); static_assert(false, "error on instantiation"); // OK because of CWG2518/P2593R1 };   struct no_copy { no_copy(const no_copy&) = delete; no_copy() = default; };   struct no_default { no_default() = delete; };   #if __cpp_static_assert >= 202306L // Not real C++ yet (std::format should be constexpr to work): static_assert(sizeof(int) == 4, std::format("Expected 4, got {}", sizeof(int))); #endif   int main() { int a, b; swap(a, b);   no_copy nc_a, nc_b; swap(nc_a, nc_b); // 1   [[maybe_unused]] data_structure ds_ok; [[maybe_unused]] data_structure ds_error; // 2 }

Possible output:

1: error: static assertion failed: Swap requires copying 2: error: static assertion failed: Data structure requires default-constructible elements 3: error: static assertion failed: Expected 4, got 2

[edit] 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 bevalid in a constant expression
CWG 2518(P2593R1) C++11 uninstantiated static_assert(false, ""); was ill-formed made well-formed

[edit] References

[edit] See also

#error shows the given error message and renders the program ill-formed(preprocessing directive)[edit]
assert aborts the program if the user-specified condition is not true. May be disabled for release builds. (function macro) [edit]
contract_assert statement (C++26) verifies an internal condition during execution[edit]
enable_if(C++11) conditionally removes a function overload or template specialization from overload resolution (class template) [edit]
Type traits (C++11) define compile-time template-based interfaces to query the properties of types
C documentation for Static assertion