Integer constant expressions
An integer constant is a value that is determined at compile time and cannot be changed at run time. An integer constant expression is an expression that is composed of constants and evaluated to a constant at compile time.
An integer constant expression is an expression that is
composed of only the following elements:
- literals
- enumerators
constvariables initialized with compile-time constant expressions or
constexprexpressions
static constdata members initialized with compile-time constant expressions or
constexprexpressions
- casts to integral types
sizeofexpressions, where the operand is not a variable length array
The sizeof operator applied to a variable
length array type is evaluated at run time, and therefore is not a
constant expression.
You must use an integer constant expression in the following
situations:
- In the subscript declarator as the description of an array bound.
- After the keyword
casein aswitchstatement. - In an enumerator, as the numeric value of an enumeration constant.
- In a bit-field width specifier.
- In the preprocessor
#ifstatement. (Enumeration constants, address constants, andsizeofcannot be specified in a preprocessor#ifstatement.)
Note:
The C++11 standard generalizes the concept of constant expressions.
For more information, see Generalized constant expressions (C++11).
The C++11 standard generalizes the concept of constant expressions.
For more information, see Generalized constant expressions (C++11).