public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/21837] New: C++/C99 standard violation in for loop
@ 2005-05-31 10:46 ahelm at gmx dot net
2005-05-31 11:16 ` Gabriel Dos Reis
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: ahelm at gmx dot net @ 2005-05-31 10:46 UTC (permalink / raw)
To: gcc-bugs
C++ standard quote:
3.3.2, paragraph 4:
Names declared in the for-init-statement, and in the condition of if, while,
for, and switch statements are local to the if, while, for, or switch statement
(including the controlled statement), and shall not be re-declared in a
subsequent condition of that statement nor in the outermost block (or, for the
if statement, any of the outermost blocks) of the controlled statement; see 6.4.
However g++/gcc happily compiles:
#include <stdio.h>
int main(void)
{
for(int i=2;i<4;i++)
{
int j = i;
int i;
i = 555;
printf("%d %d\n", i, j);
}
return 0;
}
and allows re-declaration of i.
No warning/remark/error is given.
This should trigger an error.
Maybe it could be a gcc extension to the standard(s), but then it should
be clearly marked as such.
The C99 standard is a bit less clear about this but I believe this should also
apply to C99.
Command lines used:
gxx -W -Wall -ansi -pedantic main.cpp
gcc -W -Wall -std=c99 -pedantic main.c
--
Summary: C++/C99 standard violation in for loop
Product: gcc
Version: 4.0.0
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: ahelm at gmx dot net
CC: gcc-bugs at gcc dot gnu dot org
GCC host triplet: DJGPP
GCC target triplet: MS-DOS
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21837
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Bug c++/21837] New: C++/C99 standard violation in for loop
2005-05-31 10:46 [Bug c++/21837] New: C++/C99 standard violation in for loop ahelm at gmx dot net
@ 2005-05-31 11:16 ` Gabriel Dos Reis
2005-05-31 11:48 ` [Bug c++/21837] " gdr at integrable-solutions dot net
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Gabriel Dos Reis @ 2005-05-31 11:16 UTC (permalink / raw)
To: gcc-bugzilla; +Cc: gcc-bugs
"ahelm at gmx dot net" <gcc-bugzilla@gcc.gnu.org> writes:
| C++ standard quote:
|
| 3.3.2, paragraph 4:
| Names declared in the for-init-statement, and in the condition of if, while,
| for, and switch statements are local to the if, while, for, or switch statement
| (including the controlled statement), and shall not be re-declared in a
| subsequent condition of that statement nor in the outermost block (or, for the
| if statement, any of the outermost blocks) of the controlled statement; see 6.4.
|
| However g++/gcc happily compiles:
|
| #include <stdio.h>
|
| int main(void)
| {
| for(int i=2;i<4;i++)
| {
| int j = i;
| int i;
There is duplicate of this bug in the case of C++, see PR2288.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/21837] C++/C99 standard violation in for loop
2005-05-31 10:46 [Bug c++/21837] New: C++/C99 standard violation in for loop ahelm at gmx dot net
2005-05-31 11:16 ` Gabriel Dos Reis
@ 2005-05-31 11:48 ` gdr at integrable-solutions dot net
2005-05-31 12:22 ` joseph at codesourcery dot com
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: gdr at integrable-solutions dot net @ 2005-05-31 11:48 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From gdr at integrable-solutions dot net 2005-05-31 11:16 -------
Subject: Re: New: C++/C99 standard violation in for loop
"ahelm at gmx dot net" <gcc-bugzilla@gcc.gnu.org> writes:
| C++ standard quote:
|
| 3.3.2, paragraph 4:
| Names declared in the for-init-statement, and in the condition of if, while,
| for, and switch statements are local to the if, while, for, or switch statement
| (including the controlled statement), and shall not be re-declared in a
| subsequent condition of that statement nor in the outermost block (or, for the
| if statement, any of the outermost blocks) of the controlled statement; see 6.4.
|
| However g++/gcc happily compiles:
|
| #include <stdio.h>
|
| int main(void)
| {
| for(int i=2;i<4;i++)
| {
| int j = i;
| int i;
There is duplicate of this bug in the case of C++, see PR2288.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21837
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/21837] C++/C99 standard violation in for loop
2005-05-31 10:46 [Bug c++/21837] New: C++/C99 standard violation in for loop ahelm at gmx dot net
2005-05-31 11:16 ` Gabriel Dos Reis
2005-05-31 11:48 ` [Bug c++/21837] " gdr at integrable-solutions dot net
@ 2005-05-31 12:22 ` joseph at codesourcery dot com
2005-05-31 13:16 ` pinskia at gcc dot gnu dot org
2005-05-31 17:43 ` ahelm at gmx dot net
4 siblings, 0 replies; 6+ messages in thread
From: joseph at codesourcery dot com @ 2005-05-31 12:22 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From joseph at codesourcery dot com 2005-05-31 11:48 -------
Subject: Re: New: C++/C99 standard violation in for loop
On Tue, 31 May 2005, ahelm at gmx dot net wrote:
> for(int i=2;i<4;i++)
> {
> int j = i;
> int i;
> i = 555;
> printf("%d %d\n", i, j);
> }
I don't see why you think there's any problem in C99 terms. The for
statement forms a block (6.8.5#5 first sentence); its body forms a block
whose scope is a strict subset of that of the for statement (second
sentence) and the compound statement is itself a block (whether the same
block or a different one from that of the body as body doesn't matter in
this case, but if the body is a labeled compound statement then you'd have
distinct scopes). As these are distinct blocks, they are distinct scopes;
you can declare i in the block which contains the for statement, and in
the declaration part of the for statement, and in the body of the for
statement.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21837
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/21837] C++/C99 standard violation in for loop
2005-05-31 10:46 [Bug c++/21837] New: C++/C99 standard violation in for loop ahelm at gmx dot net
` (2 preceding siblings ...)
2005-05-31 12:22 ` joseph at codesourcery dot com
@ 2005-05-31 13:16 ` pinskia at gcc dot gnu dot org
2005-05-31 17:43 ` ahelm at gmx dot net
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-05-31 13:16 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2005-05-31 13:16 -------
This is a dup of bug 2288. C99 allows this as mentioned multiple times.
*** This bug has been marked as a duplicate of 2288 ***
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |DUPLICATE
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21837
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/21837] C++/C99 standard violation in for loop
2005-05-31 10:46 [Bug c++/21837] New: C++/C99 standard violation in for loop ahelm at gmx dot net
` (3 preceding siblings ...)
2005-05-31 13:16 ` pinskia at gcc dot gnu dot org
@ 2005-05-31 17:43 ` ahelm at gmx dot net
4 siblings, 0 replies; 6+ messages in thread
From: ahelm at gmx dot net @ 2005-05-31 17:43 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From ahelm at gmx dot net 2005-05-31 17:41 -------
Subject: Re: C++/C99 standard violation in for loop
At 12:48 31/05/2005, joseph at codesourcery dot com wrote:
>
>------- Additional Comments From joseph at codesourcery dot com 2005-05-31
>11:48 -------
>Subject: Re: New: C++/C99 standard violation in for loop
>
>On Tue, 31 May 2005, ahelm at gmx dot net wrote:
>
>> for(int i=2;i<4;i++)
>> {
>> int j = i;
>> int i;
>> i = 555;
>> printf("%d %d\n", i, j);
>> }
>
>I don't see why you think there's any problem in C99 terms. The for
>statement forms a block (6.8.5#5 first sentence); its body forms a block
>whose scope is a strict subset of that of the for statement (second
>sentence) and the compound statement is itself a block (whether the same
>block or a different one from that of the body as body doesn't matter in
>this case, but if the body is a labeled compound statement then you'd have
>distinct scopes). As these are distinct blocks, they are distinct scopes;
>you can declare i in the block which contains the for statement, and in
>the declaration part of the for statement, and in the body of the for
>statement.
I knew I missed something...
The end of scope description in the "for" chapter, "end of the loop" sounded
rather nebulous to me, that's why I wasn't 100% sure about C99.
Thanks for pointing this out.
Regards,
Tony
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21837
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2005-05-31 17:41 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-31 10:46 [Bug c++/21837] New: C++/C99 standard violation in for loop ahelm at gmx dot net
2005-05-31 11:16 ` Gabriel Dos Reis
2005-05-31 11:48 ` [Bug c++/21837] " gdr at integrable-solutions dot net
2005-05-31 12:22 ` joseph at codesourcery dot com
2005-05-31 13:16 ` pinskia at gcc dot gnu dot org
2005-05-31 17:43 ` ahelm at gmx dot net
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).