Topic: A Very Interesting Problem-
Author: the_wid@my-deja.com (Tom)
Date: 2000/09/19 Raw View
On Thu, 7 Sep 2000 18:04:03 CST, Vinayak Raghuvamshi
<vs_raghuvamshi@my-deja.com> wrote:
>Look at this.
>
>main()
>{
> const int iSize = 7;
>
> char sz[iSize]; //perfectly ok !
>
>// now,
>
> int i = 7;
>
> const int iTest = i; //this is OK with the complier !
>
>// but,
>
> char szTest[iTest]; //here compiler cribs "constant expected !"
>
>}
>
>isnt it intriguing ? atleast, the compiler isnt expected to
>say "constant expected" as technically we are passing a constant
>(iTest) during array declaration....
>
>I would love to know what the compiler is exactly doing..
To initialize an array you need an integral constant-expression, which
is defined below:
(From the CD2 draft C++ standard)
"An integral constant-expression can involve only literals
(_lex.lit-
eral_), enumerators, const variables or static data members of
inte-
gral or enumeration types initialized with constant
expressions
(_dcl.init_), non-type template parameters of integral or
enumeration
types, and sizeof expressions."
You have a const variable which in one case has been initialized with
a constant expression, and in the other instance hasn't. So the first
counts as an integral constant-expression, whereas the second doesn't.
Tom
---
[ 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: Michiel Salters <salters@lucent.com>
Date: Fri, 8 Sep 2000 16:53:06 GMT Raw View
Vinayak Raghuvamshi wrote:
> Look at this.
Let me put the 'int' here (no implicit int in C++):
int
> main()
> {
> const int iSize = 7;
> char sz[iSize]; //perfectly ok !
> // now,
> int i = 7;
> const int iTest = i; //this is OK with the complier !
> // but,
> char szTest[iTest]; //here compiler cribs "constant expected !"
> }
> isnt it intriguing ? atleast, the compiler isnt expected to
> say "constant expected" as technically we are passing a constant
> (iTest) during array declaration....
> I would love to know what the compiler is exactly doing..
> -Vinayak Raghuvamshi.
Failing (to produce a clear error message).
The compiler you used should point out the error on the assignment
to iTest. This is not a standards requirement, however, but a
quality issue. The standards require at least one diagnostic for
the above program, and you get one.
However, if you would delete the offending line and write
std::cout << iTest; instead you'd still be entitled to a
diagnostic. Please try that, and if no diagnostic is given,
reply and name the compiler.
Michiel Salters
---
[ 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: Michael Rubenstein <mike@mrubenstein.com>
Date: Fri, 8 Sep 2000 16:54:35 GMT Raw View
On Thu, 7 Sep 2000 18:04:03 CST, Vinayak Raghuvamshi
<vs_raghuvamshi@my-deja.com> wrote:
>Look at this.
>
>main()
>{
> const int iSize = 7;
>
> char sz[iSize]; //perfectly ok !
>
>// now,
>
> int i = 7;
>
> const int iTest = i; //this is OK with the complier !
>
>// but,
>
> char szTest[iTest]; //here compiler cribs "constant expected !"
>
>}
>
>isnt it intriguing ? atleast, the compiler isnt expected to
>say "constant expected" as technically we are passing a constant
>(iTest) during array declaration....
>
>I would love to know what the compiler is exactly doing..
It is complying with the standard.
An array bound must be a constant integral expression.
iSize is a constant integral expression as defined in 5.19.
iTest is not. A const int is a constant integral expression only
if it is initialized with a constant integral expression. 7 is ,
but i is not a constant integral expression.
---
[ 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: Ron Natalie <ron@sensor.com>
Date: Fri, 8 Sep 2000 16:55:09 GMT Raw View
Vinayak Raghuvamshi wrote:
> main()
should explicitly declare return type to int.
> const int iSize = 7;
iSize is a constant expresion as it is a const variable initialized with a constant.
>
> char sz[iSize]; //perfectly ok !
array sizes need to be constant expressions.
> int i = 7;
i is not a constant expression because it is not const.
>
> const int iTest = i; //this is OK with the complier !
Perfectly valid. iTest is const, and can not be modified after initialized to
i. However iTest is not a constant expression because a const variable needs to
be initialized with a constant expression to count as a constant expression itself.
This is not the case here.
> char szTest[iTest]; //here compiler cribs "constant expected !"
Array sizes must be constant expressions.
There is a different between a const variable and constant expression.
constant expression is defined (5.19 of the C++ standard)
An integral constant-expression can involve only literals (2.13), enumerators, const variables or static
data members of integral or enumeration types initialized with constant expressions (8.5), non-type template
parameters of integral or enumeration types, and sizeof expressions. Floating literals (2.13.3) can
appear only if they are cast to integral or enumeration types. Only type conversions to integral or enumeration
types can be used. In particular, except in sizeof expressions, functions, class objects, pointers, or
references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall
not be used.
---
[ 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: Steve Clamage <stephen.clamage@sun.com>
Date: Fri, 8 Sep 2000 17:28:00 GMT Raw View
Vinayak Raghuvamshi wrote:
>
> main()
> {
> const int iSize = 7;
>
> char sz[iSize]; //perfectly ok !
Right, because iSize is an integral constant-expression.
>
> int i = 7;
>
> const int iTest = i; //this is OK with the complier !
Yes, iTest is an integer variable that is dynamically initialized.
Here, "const" means "cannot be modified."
>
> // but,
>
> char szTest[iTest]; //here compiler cribs "constant expected !"
Yes, because the array size must be an integral constant-expression.
Variable iTest is a const int, but because it is not initialized
by an integral constant-expression, iTest is not itself an
integral constant-expression.
Refer to C++ standard, section 5.19.
--
Steve Clamage, stephen.clamage@sun.com
---
[ 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: Ron Natalie <ron@sensor.com>
Date: Fri, 8 Sep 2000 18:18:52 GMT Raw View
Michiel Salters wrote:
>
> The compiler you used should point out the error on the assignment
> to iTest. This is not a standards requirement, however, but a
> quality issue. The standards require at least one diagnostic for
> the above program, and you get one.
No it shouldn't. First off, that's not assignment. You can't assign
to a const object. Second, there's nothing wrong with using a non-const
thing to initialize a const thing. It just isn't a constant expression.
I'm not sure what error would be appropriate.
>
> However, if you would delete the offending line and write
> std::cout << iTest; instead you'd still be entitled to a
> diagnostic.
You're still not entitled to a diagnostic? Why can't you output
a constant?
Please try that, and if no diagnostic is given,
> reply and name the compiler.
>
Every C++ compiler I've ever seen works just fine and all those that are
conforming only issue an error on the bogus array declaratoin (although I
think G++ lets you get away with it).
---
[ 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: Vinayak Raghuvamshi <vs_raghuvamshi@my-deja.com>
Date: Fri, 8 Sep 2000 18:34:45 GMT Raw View
> However, if you would delete the offending line and write
> std::cout << iTest; instead you'd still be entitled to a
> diagnostic. Please try that, and if no diagnostic is given,
> reply and name the compiler.
>
> Michiel Salters
I tried what you suggested. i removed the offending line and replaced
it with cout<<iTest. i dont get any diagnostic though..
i am using vc++ 6.0 compiler under windows2000 professional.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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 ]