constant expressions
Andrew Koenig
ark at alice.UUCP
Wed Aug 15 03:45:12 AEST 1990
In article <1916 at tkou02.enet.dec.com>, diamond at tkou02.enet.dec.com (diamond at tkovoa) writes:
> In article <5930 at darkstar.ucsc.edu> daniel at terra.ucsc.edu () writes:
>
> >Should a translator identify 1-1 as ``a constant expression evaluating
> >to zero,'' (even absent optimization)?
>
> I have added comp.std.c to the distribution for this article, because
> the C standard is almost vague on this as well.
Gee, it doesn't look vague to me. It says: (section 3.2.2.3):
An integral constant expression with the value 0, or such
an expression cast to type void *, is called a null pointer
constant. If a null pointer constant is assigned to or
compared for equality to a pointer, the constant is converted
to a pointer of that type. Such a pointer, called a null
pointer, is guaranteed to compare equal to a pointer to any
object or function.
Next: is 1-1 a constant expression? We look at section 3.4 and find
that a constant expression is a `conditional-expression' with some
restrictions:
Constant expressions shall not contain assignment, increment,
decrement, function-call, or comma operators, except where
there are contained within the operand of a sizeof operator.
An integral constant expression shall have integral type and
shall only have operands that are integer constants, enumeration
constants, character constants, sizeof expression, and
floating constants that are the immediate operands of casts.
Cast operators in an integral constant expression shall only
convert arithmetic types to integral types, except as part
of an operand to the sizeof operator.
That looks pretty plain to me. 1-1 is an integral constant expression,
and may therefore be used as a null pointer constant.
In order to allow
int* p = 1-1;
while still prohibiting
int* p = 2-1;
it is necessary for the translator to compute the value of such
expressions during compilation.
It is not necessary, however, for a C++ translator to compute the
value of constant expressions of non-integral type.
--
--Andrew Koenig
ark at europa.att.com
More information about the Comp.std.c
mailing list