Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
C++ Constant Expressions

C++ Constant Expressions

C++ requires constant expressions — expressions that evaluate to a constant — for declarations of:

  • Array bounds
  • Selectors in case statements
  • Bit-field length specification
  • Enumeration initializers

Grammar

constant-expression :
conditional-expression

The only operands that are legal in constant expressions are:

  • Literals
  • Enumeration constants
  • Values declared as const that are initialized with constant expressions
  • sizeof expressions

Nonintegral constants must be converted (either explicitly or implicitly) to integral types to be legal in a constant expression. Therefore, the following code is legal:

const double Size = 11.0;
char chArray[(int)Size];

Explicit conversions to integral types are legal in constant expressions; all other types and derived types are illegal except when used as operands to the sizeof operator.

The comma operator and assignment operators cannot be used in constant expressions.

See Also

Types of Expressions

Show:
© 2015 Microsoft