Namespaces
Variants
Actions

static_assert declaration (since C++11)

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

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 -

a contextually converted constant expression of type bool. Built-in conversions are not allowed, except for non-narrowing integral conversions to bool.

(until C++23)

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

(since C++23)
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:

  • If the message matches the syntactic requirements of unevaluated-string, the text of the message is the text of the unevaluated-string.
  • Otherwise, given the following values:
The text of the message is formed by the sequence of len code units, starting at ptr, of the ordinary literal encoding. For each integer i in [0len), ptr[i] must be an integral constant expression.
(since C++26)

[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