Share via

Assertion and User-Supplied Messages (C++)

The C++ language supports three error handling mechanisms that help you debug your application: the #error directive, the static_assert keyword, and the assert Macro, _assert, _wassert macro. All three mechanisms issue error messages, and two also test software assertions. A software assertion specifies a condition that you expect to be true at a particular point in your program. If a compile time assertion fails, the compiler issues a diagnostic message and a compilation error. If a run-time assertion fails, the operating system issues a diagnostic message and closes your application.


The lifetime of your application consists of a preprocessing, compile, and run time phase. Each error handling mechanism accesses debug information that is available during one of these phases. To debug effectively, select the mechanism that provides appropriate information about that phase:

  • The #error directive is in effect at preprocessing time. It unconditionally emits a user-specified message and causes the compilation to fail with an error. The message can contain text that is manipulated by preprocessor directives but any resulting expression is not evaluated.

  • The static_assert declaration is in effect at compile time. It tests a software assertion that is represented by a user-specified integral expression that can be converted to a Boolean. If the expression evaluates to zero (false), the compiler issues the user-specified message and the compilation fails with an error.

    The static_assert declaration is especially useful for debugging templates because template arguments can be included in the user-specified expression.

  • The assert Macro, _assert, _wassert macro is in effect at run time. It evaluates a user-specified expression, and if the result is zero, the system issues a diagnostic message and closes your application. Many other macros, such as_ASSERT and _ASSERTE, resemble this macro but issue different system-defined or user-defined diagnostic messages.

See also

#error Directive (C/C++)
assert Macro, _assert, _wassert