Bug 28981

Summary: g++ -pedantic issues error array bound not integer although it is a constant
Product: gcc Reporter: Frank Brase <brase>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED INVALID    
Severity: normal CC: bangerth, gcc-bugs
Priority: P3    
Version: 4.1.1   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Frank Brase 2006-09-08 08:28:00 UTC
g++ version: 4.1.1
target platform: linux kernel 2.6.13-15

with option "pedantic" the following line does not compile outside main:

int array3[(const unsigned short) (20.5 * 3)];

error message from compiler is:
"error: array bound is not an integer constant"

to me this is wrong because the expression "(const unsigned short) (20.5 * 3)" is an integer constant

Note: within main the same expression does compile with pedantic option

Here is a sample program:
// Begin of sample program
//
// Question: Why does array3 not compile ?
// this file demonstrates a problem in compilation with the pedantic option
//
// btspc21  > g++ -pedantic -c compiler_fail.cc
// compiler_fail.cc:18: error: array bound is not an integer constant


// float conversion alone compiles

int array1[(const unsigned short) (20.5)];

// calculation compiles

int array2[(const unsigned short) (20 * 45)];

// combination does not compile

int array3[(const unsigned short) (20.5 * 3)];

int main()
{
    // combination in main does compile

    int array4[(unsigned short) (20.5*3)];
}

// End of sample program
Comment 1 Wolfgang Bangerth 2006-10-12 00:39:11 UTC
> int array3[(const unsigned short) (20.5 * 3)];
> 
> error message from compiler is:
> "error: array bound is not an integer constant"
> 
> to me this is wrong because the expression "(const unsigned short) (20.5 * 3)"
> is an integer constant

It's actually not:
--------------------
  5.19  Constant expressions                                [expr.const]

1 In several places,  C++  requires  expressions  that  evaluate  to  an
  integral  or  enumeration  constant:  as  array  bounds  (_dcl.array_,
  _expr.new_), as case expressions (_stmt.switch_), as bit-field lengths
  (_class.bit_),  as  enumerator  initializers  (_dcl.enum_),  as static
  member  initializers  (_class.static.data_),  and   as   integral   or
  enumeration non-type template arguments (_temp.arg_).

     constant-expression:
             conditional-expression

  An    integral   constant-expression   can   involve   only   literals
  (_lex.literal_), enumerators, const variables or static  data  members
  of integral or enumeration types initialized with constant expressions
  (_dcl.init_), non-type template parameters of integral or  enumeration
  types,  and  sizeof  expressions.   Floating literals (_lex.fcon_) 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.
------------------------
Note that floating literals are only allowed if immediately cast to integral
or enumeration types. No arithmetic on them is allowed.

W.