...
- size of a bit-field member of a structure
- size of an array (except in the case of variable length arrays)
- value of an enumeration constant
- value of a
case
constant
If any of these are required, then an integer constant (which would be an rvalue) must be used.
const
-qualified objects allows the programmer to take the address of the object.
...
Also, const
-qualified integers cannot be used in locations where an integer constant is required, such as the value of a case
constant.
const:
- operates at run time
- consumes memory (though this is not too important)
- can't use in compile-time constant expression
- uses consistent syntax
- can create pointers to
- does type checking
Enumeration Constants
An enumeration constant is a member of an enumeration. Enumeration constant can be used to represent an integer constant expression that has a value representable as an int
. Unlike const
-qualified objects, enumeration constants do not require that storage is allocated for the value so it is not possible to take the address of an enumeration constant.
...
- operates at compile time
- consumes no memory (though this is not too important)
- can use in compile-time constant expression
- uses different syntax; can make mistake with ;
- can't create pointers to
- no type checking
Summary
const:
- operates at run time
- consumes memory (though this is not too important)
- can't use in compile-time constant expression
- uses consistent syntax
- can create pointers to
- does type checking
If any of these are required, then an integer constant (which would be an rvalue) must be usedThe following table summarizes some of the differences between const
-qualified objects, enumeration constants, and object-like macro definitions.
Method | Evaluated at | Consumes Memory | Viewable by Debuggers | Type Checking | Compile-time constant expression |
---|---|---|---|---|---|
Enumerations | compile time | no | yes | yes | no |
| run time | yes | yes | yes | no |
Macros | preprocessor | no | no | no | yes |
...