Thread View: comp.std.c++
5 messages
5 total messages
Started by daniel
Mon, 13 Aug 1990 00:11
constant expressions
Author: daniel
Date: Mon, 13 Aug 1990 00:11
Date: Mon, 13 Aug 1990 00:11
4 lines
569 bytes
569 bytes
Should a translator identify 1-1 as ``a constant expression evaluating to zero,'' (even absent optimization)? daniel
Re: constant expressions
Author: diamond
Date: Tue, 14 Aug 1990 04:05
Date: Tue, 14 Aug 1990 04:05
19 lines
1601 bytes
1601 bytes
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.
Re: constant expressions
Author: Doug Gwyn
Date: Tue, 14 Aug 1990 16:44
Date: Tue, 14 Aug 1990 16:44
11 lines
1095 bytes
1095 bytes
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).
Re: constant expressions
Author: Andrew Koenig
Date: Tue, 14 Aug 1990 17:45
Date: Tue, 14 Aug 1990 17:45
54 lines
2499 bytes
2499 bytes
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
Re: constant expressions
Author: Ron Guilmette
Date: Wed, 15 Aug 1990 06:38
Date: Wed, 15 Aug 1990 06:38
18 lines
1278 bytes
1278 bytes
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.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads