From 3633163082817280918
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,ec4b60e80d49f4fe
X-Google-Attributes: gidf78e5,public
From: Jerry Leichter <leichter@smarts.com>
Subject: Re: Constant expression (Was: Re: Loop invariance)
Date: 1998/08/06
Message-ID: <35C9D918.63C5@smarts.com>#1/1
X-Deja-AN: 378580556
X-NNTP-Posting-Host: fine.smarts.com
Content-Transfer-Encoding: 7bit
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <35C1FD0B.6EF2@bean.jpl.nasa.gov> <35C67E63.69530B1C@ix.netcom.com> <6q7q3c$omn$1@nnrp1.dejanews.com> <35C8A8B3.77BC@pratique.fr> <35C8C39D.41C6@wizard.net>
To: James Kuyper <kuyper@wizard.net>
X-UID: 0000000001
X-Status: $$$T
Content-Type: text/plain; charset=us-ascii
Organization: System Management ARTS
Mime-Version: 1.0
Newsgroups: comp.std.c++
Originator: clamage@taumet

| > >     const int days_in_Q1 = 31+28+31;
| >
| > A conforming C++ compiler is required to compute the sum.
| > Computation of integral constant expressions can't be
| > defered to runtime. But there is no such requirement in
| > the following example:
| >
| > const float sum = 2. + 4.;
| 
| Citations please? How could a conforming program tell?

For what, exactly?  That integer constant expressions must be evaluated
at compile time, one can tell through templates:

template <int i>
class A { ... };

Then A<3> and A<1+2> must be the same type.  Also, after

const int three = 4 - 1;

A<three> must also be the same type.  ('three' is an integral constant
expression since it is a const variable of integral type that is
initialized to an integral constant expression.)

Since template parameters can't be floats, you can't make the same
statement for float constant expressions.  The definition of integral
constant expression (5.9) allows floating point *literals* to appear if
they are cast to an integral or enumeration type; but something like
int(1.0 + 3.0) isn't required to be accepted; and if you do something
odd like:

const int Three = 1.0 + 2.0;

then 'Three' is not an integral constant expression, since its
initializing value is not (and hence A<Three> is illegal).

Hence, a C++ compiler must be able to perform integer arithmetic on
constants; and it must be able to convert a float literal to an integer;
and it must do such arithmetic and conversions in template instantia-
tion.

In other contexts, I'll quite agree that no standard-conforming program
can tell whether 'three' is evaluated at compile time, once at run-
time; once every time the variable is used (other than in a template
instantiation); or at some arbitrary combination of these.  Since the
evaluation of constant expressions can't have side-effects, a compiler
could choose to recompute them between every pair of instructions it
generated, and you couldn't tell.
							-- Jerry


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




