Topic: Constant expression (Was: Loop invariance)
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/08/06 Raw View
James Kuyper <kuyper@wizard.net> writes:
> Valentin Bonnard wrote:
> >
> > AllanW@my-dejanews.com wrote:
> >
> > > Five years ago, I would write code like this:
> > > const int days_in_Q1 = 91; // 31+28+31;
> > > instead of:
> > > const int days_in_Q1 = 31+28+31;
> > > because I knew that some compilers would do the addition at runtime.
> >
> > 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?
It can't. Explanation follows.
> The standard does
> a very precise definition of tranlation phases, but seldom explicitly
> distiguishes run-time from compile-time.
The principle of the least punishement: an implementor doesn't
make extra efforts (takes extra work/ressources) to punish
his customers. For example, if I hear someone saying that
'identifiers with the letter "j" in them should be avoided because
a compiler could slow down accesses to such identifiers', I will
answer that it's wrong under the least punishement principle:
it would require extra work to add this slowdown (the compiler
would test if the letter j is used, then punish the programmer
with the slowdown).
Does this explanation satisfies you ?
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
[ 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 ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/08/07 Raw View
Valentin Bonnard wrote:
>
> James Kuyper <kuyper@wizard.net> writes:
>
> > Valentin Bonnard wrote:
> > >
> > > AllanW@my-dejanews.com wrote:
> > >
> > > > Five years ago, I would write code like this:
> > > > const int days_in_Q1 = 91; // 31+28+31;
> > > > instead of:
> > > > const int days_in_Q1 = 31+28+31;
> > > > because I knew that some compilers would do the addition at runtime.
> > >
> > > 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?
>
> It can't. Explanation follows.
>
> > The standard does
> > a very precise definition of tranlation phases, but seldom explicitly
> > distiguishes run-time from compile-time.
>
> The principle of the least punishement: an implementor doesn't
> make extra efforts (takes extra work/ressources) to punish
> his customers. For example, if I hear someone saying that
Sure, it's a quality of implementation issue - I can't think of any
reason why any implementor would fail to make such an optimization,
except during an early prototype build of the compiler.
> 'identifiers with the letter "j" in them should be avoided because
> a compiler could slow down accesses to such identifiers', I will
> answer that it's wrong under the least punishement principle:
> it would require extra work to add this slowdown (the compiler
> would test if the letter j is used, then punish the programmer
> with the slowdown).
>
> Does this explanation satisfies you ?
No. You said that that "Computation of integral constant expressions
can't be defered to runtime." In this newsgroup, as an explanation of
what "a conforming C++ compiler is" required to do, that means that it's
prohibited by the standard, not just by the marketplace. So where in the
standard does it require that ALL integral constant expressions must be
evaluated at compile time?
[ 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 ]
Author: Jerry Leichter <leichter@smarts.com>
Date: 1998/08/06 Raw View
| > > 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 ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/08/06 Raw View
Jerry Leichter wrote:
>
> | > > 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:
I agree that in SOME circumstances an integral constant expression must
be evaluated in order to translate the code. However, none of those
circumstances occured in Allen's example.
[ 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 ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/08/05 Raw View
AllanW@my-dejanews.com wrote:
> Five years ago, I would write code like this:
> const int days_in_Q1 = 91; // 31+28+31;
> instead of:
> const int days_in_Q1 = 31+28+31;
> because I knew that some compilers would do the addition at runtime.
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.;
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
[ 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 ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/08/05 Raw View
Valentin Bonnard wrote:
>
> AllanW@my-dejanews.com wrote:
>
> > Five years ago, I would write code like this:
> > const int days_in_Q1 = 91; // 31+28+31;
> > instead of:
> > const int days_in_Q1 = 31+28+31;
> > because I knew that some compilers would do the addition at runtime.
>
> 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? The standard does
a very precise definition of tranlation phases, but seldom explicitly
distiguishes run-time from compile-time.
[ 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 ]