As far as I know, the ISO C standard is strict about initializers for object with static storage duration in C11 6.7.9 Initialization
All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.
But both GCC/Clang accepted the following code:
const int i = 3; // const here should not make i a constant expression
static int j = i;
Even with -Wall -Wextra -Werror -pedantic-errors
, the compilers above are not giving me any complaint.
However, these compilers do realize that i
was not a constant expression. For example, Clang gave me:
error: size of static array must be an integer constant expression
for the following code:
const size_t sz = 3;
static int a[sz];
Am I getting anything wrong here?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…