On 22 Apr 2007 17:16:57 -0700, Peter Nilsson <ai***@acay.com .au>
wrote:
Ian Collins <ian-n...@hotmail.co mwrote:
Jack Klein wrote:
Despite what others have said, the C language standard does not
require a compiler to evaluate constant expressions at compile time
Vacuously true.
unless it needs to use the value at compile time, as in Ian's example
of using it to define the size of an array.
Thanks for the clarification Jack, I wasn't aware of that "unless".
What "unless"?
The standard simply says (Epmhasis mine): "A constant expression _can_
be evaluated during _translation_ rather than runtime..." There is no
"must"
or "shall" precluding runtime evaluation of all constant expressions.
Expectations of QoI is the only guide. But when it comes to constant
expressions, expectations are pretty high these days. :-)
True. But for some _integer_ constant expressions:
- the ICE specifying bitfield width in a struct must not be greater
than the 'normal' width (before C99, of [u]int) nor less than zero and
if zero must not be named; this is a constraint and so any violation
of it must be diagnosed. It's hard to do that without evaluating the
constant expression at compile time, unless you weasel out with a
blanket diagnostic like "this program may contain errors".
- the ICE specifying the value of an enumeratee must be in range of
int, as a constraint. This might not be too hard to defer, except that
the enumeratee can in turn be used in other ICEs which may need to be
evaluated.
- if the (integer) expression for array bound in a declarator is
constant it must be positive as a constraint; plus whether two array
types are compatible depends on whether their bounds are ICEs with the
same value, and compatibility is in some constraints.
- (ICEs for) case labels in a switch must be distinct, a constraint.
And as already noted, (integer) constant expressions in #if or #elif
must be evaluated at preprocessing time, at least in principle BEFORE
compile time, because
#if 0
#include "nonexistentfil e"
#endif
int main (void) { return 0; }
must succeed (or at least not fail because of that construct).
OTOH it may actually make sense to defer floating-point constant
expressions in cases of cross compilation where the target floating
point arithmetic is different from the host and not worth the trouble
of simulating or (God forbid) not documented well enough to simulate.
- formerly david.thompson1 || achar(64) || worldnet.att.ne t