Several varieties of expressions are known as constant expressions.
The expression following #if or #elif must expand to.
defined
Character constants, when evaluated in #if
-expressions, may be interpreted in the source character set, the execution character set, or some other implementation-defined character set.
Integer arithmetic in | (since C99) |
An integer constant expression is an expression that consists only of.
| (since C11) |
Integer constant expressions are evaluated at compile time. The following contexts require expressions that are known as integer constant expressions':
case
label of a switch statement
| (since C99) |
| (since C11) |
Expressions that are used in the initializers of objects with static and thread_local storage duration must be expressions that may be one of the following.
| (since C11) |
NULL
Unlike with integer constant expressions, static initializer expressions are not required to be evaluated at compile time; the compiler is at liberty to turn such initializers into executable code which is invoked prior to program startup.
static int i = 2 || 1 / 0; // initializes i to value 1
The value of a floating-point static initializer is never less accurate than the value of the same expression executed at run time, but it may be better.
Arithmetic constant expressions of floating-point types that are not used in static initializers are always evaluated as-if during run-time and are affected by the current rounding (if FENV_ACCESS is on) and report errors as specified in math_errhandling.
void f(void) { #pragma STDC FENV_ACCESS ON static float x = 0.0/0.0; // static initializer: does not raise an exception float w[] = { 0.0/0.0 }; // raises an exception float y = 0.0/0.0; // raises an exception double z = 0.0/0.0; // raises an exception }
If an expression evaluates to a value that is not representable by its type, it cannot be used as a constant expression.
C++ documentation for Constant expressions |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/language/constant_expression