From: krishna on
const int i[] = {10, 12, 18, 20}
//The below isn't legal, the text says because the array above being
const, it can't be used at compile time, and the error is "error
C2057: expected constant expression". It seems (I tried it) it can't
be done whether the array above is const or not, the (here float)array
size value must be a constant rather than say an expression. Is this
true?

float f[i[1]];

what was the author (Bruce Eckel) trying to say (page 338 Vol 1 second
Edition, Thinking in c++) by giving const array example in this case?

-Krishna.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: John H. on
krishna wrote:
> const int i[] = {10, 12, 18, 20}
> //The below isn't legal, the text says because the array above being
> const, it can't be used at compile time, and the error is "error
> C2057: expected constant expression". It seems (I tried it) it can't
> be done whether the array above is const or not, the (here float)array
> size value must be a constant rather than say an expression. Is this
> true?

According to C++, an array size must be a "integral constant
expression" which has a particular meaning defined by the standard
(5.19). Many of things you might think of as integral constant
expression are (e.g. const int x = 3;). Accessing an element from an
array does not qualify (even though it is an array of const ints).
It seems to me like the compiler should be able to determine the value
at compile time and thus use it as an array size. I am not sure of
the reasoning of excluding it from things allowed as integral constant
expressions.

Another related concept is variable length arrays. This is a feature
of C99 that many C++ compilers support. With this feature, the size
of the array needn't be a constant expression, and the code you
suggested would work.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: liam on

> array size value must be a constant rather than say an expression. Is this
> true?
Yes it must be a compile time constant as VLA's are not supported by C+
+.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alp Mestan on
On Nov 13, 3:11 am, "John H." <oldman_fromt...(a)yahoo.com> wrote:
> According to C++, an array size must be a "integral constant
> expression" which has a particular meaning defined by the standard
> (5.19). Many of things you might think of as integral constant
> expression are (e.g. const int x = 3;). Accessing an element from an
> array does not qualify (even though it is an array of const ints).

Absolutely. Only a compile-time array, like boost::mpl::vector_c, can
let you do that, but yeah it's not the same kind of container anymore.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]