An array bound must be an integral constant expression, see 8.3.4 [dcl.array]/1 (same wording in C++03 and C++11):
If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.
In C++03 an integral constant expression cannot be initialized by a floating literal unless cast to integral type, see the last sentence of 5.19 [expr.const]/1:
An integral constant-expression can involve only literals (2.13), enumerators, const
variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof
expressions. Floating literals (2.13.3) can appear only if they are cast to integral or enumeration types.
This means that in C++03 i
is not an integral constant expression, so cannot be used as an array bound.
GCC and Clang allow variable-length arrays as an extension to C++03, so it compiles with a non-constant bound, but you get a warning with -pedantic
. Changing the constant's initializer to cast it to integral type makes i
a valid integral constant expression:
const int i = (int) 1.0;
With that change the array is no longer variable length and there is no warning even with -pedantic
.
In C++11 5.19 [expr.const]/3 says:
A literal constant expression is a prvalue core constant expression of literal type, but not pointer type. An integral constant expression is a literal constant expression of integral or unscoped enumeration type.
The preceding (quite lengthy) paragraphs describe the rules for core constant expressions, but basically in C++11 the double initializer does not prevent i
being a core constant expression, even without a cast, so it is an integral constant expression and therefore a valid array bound, so no warning.