Xref: utzoo comp.std.c++:189 comp.std.c:3500 Path: utzoo!utgpu!watserv1!watmath!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.std.c++,comp.std.c Subject: Re: constant expressions Message-ID: <11186@alice.UUCP> Date: 14 Aug 90 17:45:12 GMT References: <5930@darkstar.ucsc.edu> <1916@tkou02.enet.dec.com> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 54 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