[comp.std.c++] constant expressions

daniel@terra.ucsc.edu (08/13/90)

Should a translator identify 1-1 as ``a constant expression evaluating
to zero,'' (even absent optimization)?

daniel

diamond@tkou02.enet.dec.com (diamond@tkovoa) (08/14/90)

In article <5930@darkstar.ucsc.edu> daniel@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.  If C++ maintains
compatibility with C here, then my best guess is:
  A translator does not have to compute the constant expression at
compile time, but it does have to remember that the expression is
constant.  When the value is cast (or automatically converted) to a
pointer, the compiled code has to remember that it was a constant,
check if the computation yields zero, and if so then convert it to
a null pointer.
  In other words, it is not necessary to do optimization, but if it
is not done, the result has to be "as if" optimization were done. :-)
-- 
Norman Diamond, Nihon DEC     diamond@tkou02.enet.dec.com
This is me speaking.  If you want to hear the company speak, you need DECtalk.

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/14/90)

In article <1916@tkou02.enet.dec.com> diamond@tkou02.enet.dec.com (diamond@tkovoa) writes:
>In article <5930@darkstar.ucsc.edu> daniel@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.

The C standard is not at all vague about this; see section 3.4.
"1-1" is definitely an integral constant expression with value zero.
It may be used any place that the integral constant "0" could be used,
including in constructing a null pointer constant (see section 3.2.2.3).

ark@alice.UUCP (Andrew Koenig) (08/15/90)

In article <1916@tkou02.enet.dec.com>, diamond@tkou02.enet.dec.com (diamond@tkovoa) writes:
> In article <5930@darkstar.ucsc.edu> daniel@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@europa.att.com

rfg@NCD.COM (Ron Guilmette) (08/15/90)

In article <1916@tkou02.enet.dec.com> diamond@tkou02.enet.dec.com (diamond@tkovoa) writes:
>In article <5930@darkstar.ucsc.edu> daniel@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.  If C++ maintains
>compatibility with C here, then my best guess is:
>  A translator does not have to compute the constant expression...

Neither the C standard nor E&S are vague about this at all.  Clearly,
1-1 is "a constant expression evaluating to zero".
-- 

// Ron Guilmette  -  C++ Entomologist
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.