SasQ wrote:

Dnia Sat, 17 Mar 2007 01:26:16 +0000, Mike Wahler napisa?(a):

>>Can variables be used for array size in C++?

Not with standard C++. The specified array dimension

must be a constant expression.

Hmm.. so what about this?

>>foo (const int x)

{

type arr[x];

}

That x is not a constant expression [5.19/1]:

In several places, C + + requires expressions that evaluate to an integral

or enumeration constant: as array bounds (8.3.4, 5.3.4), as case

expressions (6.4.2), as bit-field lengths (9.6), as enumerator

initializers (7.2), as static member initializers (9.4.2), and as integral

or enumeration non-type template arguments (14.3).

constant-expression:

conditional-expression

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. Only type conversions to integral or

enumeration types can be used. In particular, except in sizeof

expressions, functions, class objects, pointers, or references shall not

be used, and assignment, increment, decrement, function-call, or comma

operators shall not be used.

In particular, note that const variables only qualify as constant

expressions if they are initialized from another constant expression.

Best

Kai-Uwe Bux