Topic: A Constant Array of Constant Ints


Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sun, 24 Apr 1994 23:14:27 GMT
Raw View
In article <2ohhct$sbr@solaris.cc.vt.edu> pfitz@corona.math.vt.edu (Paul Fitzpatrick) writes:
>How do you declare a constant array of constant ints?
>
>None of these options worked :
>  const int SwitchValues[] ...

 I think this was resolved at San Diego. You CANT HAVE
a const array -- it makes no sense, all arrays are immutable in vaccuo
because there are no operations you can do on an array other than convert
it to a pointer to the first element.

 So the committee decided that your example case means
an array of const ints. In particular:

 typedef int IntArray[2];
 const IntArray x = {1,2}; // OK
 int * px = x; // error, cant convert const int* to int*

I do not think an array element can ever be a constant expression,
which has very little to do with 'const'. A constant expression
includes "a const integral type initialised by a constant expression".
I dont believe an array can qualify. It would be absurd for
array elements to qualify:

 const int n[2]= {1, sin(2) };

We just cant have 'n[0]' being a constant expression while
n[1] is not.

If you think this is complicated and silly, I agree. The committee
is reluctant to change the language to work the way it should,
which is to have a proper syntax for compile time constants:

 define int x = 1;

As a result, its very hard to tell whats a constant expression
and what is not. Its so hard the definition in the ARM is wrong.
[Its a bit subtle: a good exercise in Standardese to find the problem
and fix it]



--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: pfitz@corona.math.vt.edu (Paul Fitzpatrick)
Date: 13 Apr 1994 19:32:45 GMT
Raw View
How do you declare a constant array of constant ints?

I was trying to do this :

int SwitchVals[] = { 12345, 1435, 1567 };

...


switch (Value) {

   case SwitchVals[0] :
      ...

   case SwitchVals[1] :


...   And so on.  The compiler complains (correctly) that the case
statement needs a constant expression.  However, I can't seem to find
out how to tell the compiler that this array will never change, and so
is really constant.

None of these options worked :
  const int SwitchValues[] ...

  int const SwitchValues[] ...

  typedef const int cint;
  cint SwitchValues[];

  Any Ideas?


Later
Paul
pfitz@vt.edu





Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 14 Apr 1994 17:10:50 -0500
Raw View
pfitz@corona.math.vt.edu (Paul Fitzpatrick) writes:

>How do you declare a constant array of constant ints?

>I was trying to do this :

>int SwitchVals[] = { 12345, 1435, 1567 };

>...


>switch (Value) {

>   case SwitchVals[0] :
>      ...

>   case SwitchVals[1] :


>...   And so on.  The compiler complains (correctly) that the case
>statement needs a constant expression.  However, I can't seem to find
>out how to tell the compiler that this array will never change, and so
>is really constant.

>None of these options worked :
>  const int SwitchValues[] ...

>  int const SwitchValues[] ...

>  typedef const int cint;
>  cint SwitchValues[];

>  Any Ideas?

An array element value is NOT a compile time constant. What you
could do is something like:

const int SwitchVals1 = 12345;
const int SwitchVals2 = 1435;
const int SwitchVals3 = 1567;

int SwitchVals[] = { SwitchVals1, SwitchVals2, SwitchVals3 };

...

  case SwitchVals1:

...

I think this should be accepted, but am not sure.