Assertions and user-supplied messages
the C + + language supports the three error-handling mechanisms that help you debug your application: #error directives, static_assert keywords, and assert (CRT) macros. All three mechanisms emit error messages, and two of them also test software assertions. A software assertion specifies a condition that should be met at a particular point in the program. If the compile-time assertion fails, the compiler issues a diagnostic message and a compilation error. If the Run-time assertion fails, the operating system issues a diagnostic message and closes the application.
Note
The lifetime of an application consists of preprocessing, compilation, and Run-time phases. Each error-handling mechanism accesses the debug information that is available in one of these three phases. To debug effectively, select the mechanism that provides appropriate information about the phase:
#error instructions are valid when preprocessing. It will unconditionally emit the user-specified message and cause the compilation to fail due to an error. The message can contain text that is manipulated by preprocessor directives, but no generated expressions are evaluated.
The Static_assert declaration is valid at compile time. It tests the software assertion that is represented by a user-specified integer expression that can be converted to a Boolean value. If the expression evaluates to 0 (false), the compiler emits the user-specified message and the compilation fails due to an error.
Static_assert declarations are particularly useful for debugging templates, because template parameters can be contained in user-specified expressions.
ASSERT (CRT) macros are valid at run time. It computes the expression specified by the user, and if the result is zero, the system emits a diagnostic message and closes the application. Many other macros, such as _assert and _asserte, are similar to this macro, but they emit different system-defined or user-defined diagnostic messages.
Static_assert
test the software assertion at compile time. If the specified constant expression is false, the compiler displays the specified message and the compilation fails with the error C2338; otherwise, the declaration does not work.
Grammar
Static_assert (
constant-expression,
string-literal
);
Parameters
Parameters |
Description |
Constant-expression |
An integer constant expression that can be converted to a Boolean value. If the evaluated expression is 0 (false), the String-literal argument is displayed and the compilation fails because of an error. If the expression is not 0 (true), the Static_assert declaration is invalid. |
String-literal |
The message displayed when the Constant-expression parameter is zero. The message is a string in the basic character set of the compiler, that is, not a multi-byte or wide character. |
Note
The constant-expression parameter of the Static_assert declaration represents the software assertion. A software assertion specifies a condition that should be met at a particular point in the program. If this condition is met, the Static_assert declaration is invalid. If this condition is not met, the assertion fails, the compiler displays the message in the String-literal parameter, and the compilation fails because of an error.
Static_assert declares that a software assertion is tested at compile time. Instead, assert (CRT) macros test software assertions at run time, and can result in increased space and time spent at run time. Because template parameters are contained in the constant-expression parameter, static_assert declarations are useful for debugging templates.
When a declaration is encountered, the compiler checks to see if there is a syntax error in the Static_assert declaration. If the compiler does not rely on template parameters, the compiler evaluates the Constant-expression parameter immediately. Otherwise, the compiler calculates the constant-expression parameter when the template is instantiated. Therefore, when a declaration is encountered, the compiler may publish one diagnostic message at a time, as well as when instantiating the template.
You can use the Static_assert keyword in a namespace, class, or block scope. (Since the Static_assert keyword can be used in a namespace-wide context, it is technically a declaration, even if it does not introduce a new name into the program.) )
Description
In the following example, the Static_assert declaration has a namespace scope. Because the compiler knows the size of type void *, an expression can be evaluated immediately.
Example
Static_assert (sizeof (void *) = = 4, "64-bit code generation is not supported.");
Description
In the following example, the Static_assert declaration has a class scope. Static_assert Verify that the template parameter is a pure old data (POD) type. The compiler checks the declaration when declaring the Static_assert declaration, but does not compute the constant-expression parameter until the Basic_string class template is instantiated in main ().
Example
#include <type_traits>
#include <iosfwd>
namespace std {
template <class CharT, class Traits = std::char_traits<chart> >
class basic_string {
Static_assert (tr1::is_pod<chart>:: Value,
"Template argument CharT must is a POD type in class Template basic_string");
// ...
};
}
struct Nonpod {
nonpod (const Nonpod &) {}
Virtual ~nonpod () {}
};
int main ()
{
std::basic_string<char> bs;
}
Description
In the following example, the Static_assert declaration has a block scope. Static_assert Verify that the size of the vmpage structure is equal to the virtual memory page size of the system.
Example
#include <sys/param.h>//defines PAGESIZE
class Vmmclient {public
:
struct Vmpage {//...
};
int check_pagesize () {
static_assert (sizeof (vmpage) = pagesize,
"Struct vmpage is the must size as a System virtual memory page. ");
// ...
}
// ...
};