Topic: compile time consts


Author: jason@cygnus.com (Jason Merrill)
Date: Sat, 24 Sep 1994 19:09:22 GMT
Raw View
>>>>> Ronald F Guilmette <rfg@netcom.com> writes:

> In article <weissmanCwHpDL.LD5@netcom.com> weissman@netcom.com (Bob Weissman) writes:
>> Because "(int) sqrt((double) N)" is not a constant-expression, neither is "Q".

> Can _anyone_ provide a chapter and verse citation to support this conclusion?
> If so, I would greatly appreciate it.  Thanks.

[expr.const].  From the September WP:

1    In  several places, C++ requires expressions that evaluate to an inte-
gral constant: as array  bounds  (_dcl.array_),  as  case  expressions
(_stmt.switch_), as bit-field lengths (_class.bit_), and as enumerator
initializers (_dcl.enum_).
        constant-expression:
                conditional-expression
An   integral   constant-expression   can   involve   only    literals
(_lex.literal_),  enumerators, const values of integral types initial-
ized with constant expressions (_dcl.init_), and  sizeof  expressions.
Floating  constants (_lex.fcon_) must be cast to integral types.  Only
type conversions to integral types may be used.  In particular, except
in  sizeof  expressions, functions, class objects, pointers, or refer-
ences cannot be used, and assignment, increment, decrement,  function-
call, or comma operators cannot be used.

Jason




Author: ddr@dcs.ed.ac.uk (Douglas Rogers)
Date: Mon, 26 Sep 1994 09:56:31 GMT
Raw View
Ronald F Guilmette writes:
In article <rfgCwMJB4.E1s@netcom.com> rfg@netcom.com (Ronald F. Guilmette) writes:


rfg> In article <weissmanCwHpDL.LD5@netcom.com> weissman@netcom.com
rfg> (Bob Weissman) writes:

>> Because "(int) sqrt((double) N)" is not a constant-expression,
>> neither is "Q".

rfg> Can _anyone_ provide a chapter and verse citation to support this
rfg> conclusion?  If so, I would greatly appreciate it.  Thanks.

Unfortunately I don't have a copy of the draft standard, and I can
find no mention of the problem or suitable exasmples in the ARM but the
implication is that the compiler may need some compile time capability
to evaluate expressions.  The idea of const in C++ is to enable a
const value to be used within a static decleration.

in C this is illegal but in C++ encouraged:-

  const int MAX_STRING = 61;
  char string[MAX_STRING];

Now this form is not in dispute, in C this is not a problem as #define
is used ie:-

   #define MAX_STRING  61

   char string[MAX_STRING];

No worries, but now if we use an expression, in C the rules are clear,
the preprocessor must have certain mathematical capability ie:-

    #define STRING_LEN 60
    #define MAX_STRING STRING_LEN + 1;

    char string[MAX_STRING];


If we do the same in C++ then we need to know - what expressions can
be evaluated at compile time. Can this be?

     const int STRING_LEN = 60;
     const int MAX_STRING = STRING_LEN + 1;

Does this only apply to built in types? What about expressions in the
standard library as in the example above?





--
Douglas

---
=============================================================================
Douglas Rogers  MAIL: ddr@dcs.ed.ac.uk  Tel: +44 31-650 5172 (direct dial)
                                        Fax: +44 31-667 7209
============================= Mostly harmless ===============================




Author: mhh@b4pph108.rus.uni-stuttgart.de (Mark Hollomon)
Date: 26 Sep 1994 17:40:37 GMT
Raw View
In article <DDR.94Sep26105635@drongs.dcs.ed.ac.uk>,
Douglas Rogers <ddr@dcs.ed.ac.uk> wrote:
>Ronald F Guilmette writes:
>No worries, but now if we use an expression, in C the rules are clear,
>the preprocessor must have certain mathematical capability ie:-
>
>    #define STRING_LEN 60
>    #define MAX_STRING STRING_LEN + 1;
>
>    char string[MAX_STRING];

Actually, this doesn't require the preprocessor to know anything about expressions.
what the compiler sees is:

      char string[60 + 1];

Try the -P option on your compiler.

---- Mark Hollomon




Author: kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425)
Date: 26 Sep 1994 19:09:45 GMT
Raw View
In article <rfgCwMJB4.E1s@netcom.com> rfg@netcom.com (Ronald
F. Guilmette) writes:

|> In article <weissmanCwHpDL.LD5@netcom.com> weissman@netcom.com (Bob Weissman) writes:
|> >In article <1994Sep19.110228.3591@fmrco.uucp>,
|> >David Binderman <dcb@lovat.fmrco.com> wrote:
|> >>I claim the following is legal code. Three compilers disagree with me.
|> >>Who is correct ?

|> >The compilers are.

|> >>#include <math.h>

|> >>const int N = 1000;
|> >>const int Q = (int) sqrt( (double) N);
|> >>int a[ Q];

|> >See ARM 8.2.4, then see ARM 5.19.  The expression inside the brackets
|> >must be a constant-expression.

|> This part, I believe.

|> >Because "(int) sqrt((double) N)" is not a constant-expression, neither is "Q".

|> Can _anyone_ provide a chapter and verse citation to support this conclusion?
|> If so, I would greatly appreciate it.  Thanks.

In the ARM, section 5.19: "A constant-expression can involve only
literals, enumerators, const values of integral types initialized with
constant expressions, and sizeof expressions."

No mention of function calls, so the initialization expression of Q is
not a constant expression.  And since Q is not initialized with a
constant expression, an expression containing Q is not a constant
expression.

And of course, in section 8.2.4, the syntax for an array declaration
requires a constant-expression.
--
James Kanze      Tel.: (+33) 88 14 49 00     email: kanze@lts.sel.alcatel.de
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung






Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sat, 24 Sep 1994 07:54:40 GMT
Raw View
In article <weissmanCwHpDL.LD5@netcom.com> weissman@netcom.com (Bob Weissman) writes:
>In article <1994Sep19.110228.3591@fmrco.uucp>,
>David Binderman <dcb@lovat.fmrco.com> wrote:
>>I claim the following is legal code. Three compilers disagree with me.
>>Who is correct ?
>
>The compilers are.
>
>>#include <math.h>
>>
>>const int N = 1000;
>>const int Q = (int) sqrt( (double) N);
>>int a[ Q];
>
>See ARM 8.2.4, then see ARM 5.19.  The expression inside the brackets
>must be a constant-expression.

This part, I believe.

>Because "(int) sqrt((double) N)" is not a constant-expression, neither is "Q".

Can _anyone_ provide a chapter and verse citation to support this conclusion?
If so, I would greatly appreciate it.  Thanks.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -




Author: kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425)
Date: 20 Sep 1994 16:31:34 GMT
Raw View
In article <1994Sep19.110228.3591@fmrco.uucp> dcb@lovat.fmrco.com
(David Binderman) writes:

|> I claim the following is legal code. Three compilers disagree with me.
|> Who is correct ?

Three against one.  You loose.

|> #include <math.h>

|> const int N = 1000;
|> const int Q = (int) sqrt( (double) N);
|> int a[ Q];

See section 5.19 of the ARM: "A constant-expression can involve only
literals, enumerators, const values of integral types initialized with
constant expressions, and sizeof expressions."  No function calls.  So
Q is not a constant-expression (since it is not initialized with a
constant expression).

Finally section 8.4 clearly says that the dimension specifier of the
array must be a constant expression.
--
James Kanze      Tel.: (+33) 88 14 49 00     email: kanze@lts.sel.alcatel.de
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung






Author: dcb@lovat.fmrco.com (David Binderman)
Date: Mon, 19 Sep 1994 11:02:28 GMT
Raw View
Hello there,

I claim the following is legal code. Three compilers disagree with me.
Who is correct ?

#include <math.h>

const int N = 1000;
const int Q = (int) sqrt( (double) N);
int a[ Q];

Regards

David C Binderman BSc MSc      +44 171 975 4723         dcb@lovat.fmrco.com





Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 19 Sep 1994 10:14:33 -0500
Raw View
dcb@lovat.fmrco.com (David Binderman) writes:

>Hello there,

>I claim the following is legal code. Three compilers disagree with me.
>Who is correct ?

>#include <math.h>

>const int N = 1000;
>const int Q = (int) sqrt( (double) N);
>int a[ Q];

Your code is incorrect. With most compilers, Q cannot be
evaluated at compile time, because its value involves a
call to a library routine, sqrt. Therefore the compiler
cannot determine the size of array a.