Bug 28981 - g++ -pedantic issues error array bound not integer although it is a constant
Summary: g++ -pedantic issues error array bound not integer although it is a constant
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-08 08:28 UTC by Frank Brase
Modified: 2006-10-12 00:39 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.