Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
static_assert(constant-expression, string-literal);

According to the C++ 0X draft standard, the constant-expression in a static assert declaration is a constant expression that can be converted to bool at compile timesection 6.7.10 of the C1X draft standard:

The constant expression shall be an integer constant expression. If the value of the

...


constant expression compares unequal to 0, the declaration has no effect. Otherwise, the

...


constraint is violated and the implementation shall produce a diagnostic message

...

that
includes the text of the string literal, except that characters not in the basic source
character set are not required to appear in the message.

This means that if constant-expression is true, nothing will happen. However, if constant-expression is false, an error message containing string-literal) is issued will be output at compile-time.

Code Block
/* Passes */
static_assert(
  sizeof(int) <= sizeof(void*), 
  "sizeof(int) <= sizeof(void*)"
); 

/* Fails */
static_assert(
  sizeof(double) <= sizeof(int), 
  "sizeof(double) <= sizeof(int)"
);

...