This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[C++] enums in constant integral expressions


Hi,
In an integral constant expression, can an implicit promotion from
enumeration type to integral type [4.5]/2 be inserted?

In several places a constant expresion of integral type is needed
1) null pointer constant (expression must be of value zero) [4.10]/1
2) array bounds declaration [8.3.4]/1
3) array new [5.3.4]/6

The attached testcase tries all these instances with an expression
of enumeral type. It gives errors for cases 1 and 3, but not 2. I can
see nothing in [8.3.4] which makes that different from the other
cases in this regard.

I see core issue 74 (http://www.informatik.hu-berlin.de/~loewis/corer8.html#74)
talks about array new, and suggests a resolution
allowing `enumeration or integral type'. If that _is_ a defect, how
come, [8.3.4] is ok? that says `integral constant expression' too.

IMHO, an implicit conversion is already allowable by the standard.
Clause 4/1 says `A standard conversion sequence will be applied to an an
expression if necessary to convert it to a required destination type.'

nathan@manao:205>g++ -ansi -pedantic -c nathan102.C
nathan102.C: In function `void foo()':
nathan102.C:19: argument passing to `void *' from `E'
nathan102.C:21: warning: size in array new must have integral type
nathan102.C:22: warning: size in array new must have integral type

nathan
-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
Never hand someone a gun unless you are sure where they will point it
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk
// Build don't link:

// Copyright (C) 2000 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 12 Feb 2000 <nathan@acm.org>

// Integral constant expressions [expr.const]/1 [5.19] of value zero are null
// pointer constants, [conv.ptr]/1 [4.10] and can be implicitly converted to
// pointer type. Enumerations can be implicitly promoted to integral type.
// [conv.prom]/2 [4.5]. Ergo an enumeration of value zero, is a null pointer
// constant.


enum E {e = 0, f = 1};

void fn (void *);

void foo ()
{
  fn (e);                       // gets bogus error - XFAIL *-*-*
  int ary[f];
  int *p0 = new int[f];         // gets bogus error - XFAIL *-*-*
  int (*p1)[1] = new int[f][1]; // gets bogus error - XFAIL *-*-*
}

void baz ()
{
  fn (e + 0);
  int ary[f + 0];
  int *p0 = new int[f + 0];
  int (*p1)[1] = new int[f + 0][1];
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]