So the first is illegal because an array must have a compile-time known bound, and i[3]
, while strictly speaking known at compile time, does not fulfill the criteria the language sets for "compile-time known".
The second is also illegal for the same reason.
Both cases, however, will generally be accepted by GCC because it supports C99-style runtime-sized arrays as an extension in C++. Pass the -pedantic
flag to GCC to make it complain.
Edit: The C++ standard term is "integral constant expression", and things qualifying as such are described in detail in section 5.19 of the standard. The exact rules are non-trivial and C++11 has a much wider range of things that qualify due to constexpr
, but in C++98, the list of legal things is, roughly:
- integer literals
- simple expressions involving only constants
- non-type template parameters of integral type
- variables of integral type declared as
const
and initialized with a constant expression