public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* scoping issue
@ 2008-09-04 13:34 Jason_Haynberg
  2008-09-04 13:41 ` Andrew Haley
  2008-09-04 13:41 ` me22
  0 siblings, 2 replies; 3+ messages in thread
From: Jason_Haynberg @ 2008-09-04 13:34 UTC (permalink / raw)
  To: gcc-help

This strikes me as a bug.  What do you think?  It seems the first program should get the same error as the second?

$ cat t.cpp
#include <iostream>
using namespace std;

main()
{
   for (int i = 0; i < 5; i++) {
      cout << i << endl;
      double i = 10.0;
      cout << i << endl;
   }
}

$ g++ t.cpp

$ a.out
0
10
1
10
2
10
3
10
4
10

$ cat t.cpp
#include <iostream>
using namespace std;

main()
{
      int i = 0;
      cout << i << endl;
      double i = 10.0;
      cout << i << endl;
}

$ g++ t.cpp
t.cpp: In function `int main()':
t.cpp:9: conflicting types for `double i'
t.cpp:7: previous declaration as `int i'

$ g++ -v
Reading specs from /usr/local/gcc-3.2.3/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.2.3/specs
Configured with: ./configure --prefix=/usr/local/gcc/gcc-3.2.3
Thread model: posix
gcc version 3.2.3

$ uname -a
Linux zolataylor 2.6.5-7.282-bigsmp #1 SMP Tue Aug 29 10:40:40 UTC 2006 i686 athlon i386 GNU/Linux

$ cat /etc/issue

Welcome to SUSE LINUX Enterprise Server 9 (i586) - Kernel \r (\l).



Jay Haynberg

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: scoping issue
  2008-09-04 13:34 scoping issue Jason_Haynberg
@ 2008-09-04 13:41 ` Andrew Haley
  2008-09-04 13:41 ` me22
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Haley @ 2008-09-04 13:41 UTC (permalink / raw)
  To: Jason_Haynberg; +Cc: gcc-help

Jason_Haynberg wrote:
> This strikes me as a bug.  What do you think?  
> It seems the first program should get the same error as the second?

Your first program is equivalent to

main()
{
  {
    int i;
    for (i = 0; i < 5; i++) {
      cout << i << endl;
      double i = 10.0;
      cout << i << endl;
    }
  }
}

Andrew.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: scoping issue
  2008-09-04 13:34 scoping issue Jason_Haynberg
  2008-09-04 13:41 ` Andrew Haley
@ 2008-09-04 13:41 ` me22
  1 sibling, 0 replies; 3+ messages in thread
From: me22 @ 2008-09-04 13:41 UTC (permalink / raw)
  To: Jason_Haynberg; +Cc: gcc-help

On Thu, Sep 4, 2008 at 09:33, Jason_Haynberg <haynberg@sig.com> wrote:
> This strikes me as a bug.  What do you think?  It seems the first program should get the same error as the second?
>
>   for (int i = 0; i < 5; i++) {
>      cout << i << endl;
>      double i = 10.0;
>      cout << i << endl;
>   }
>

This is just intuition, but my understanding is that the double is
defined inside the for loop body block, and the int is defined outside
of it (it has to be, to persist its value), so there are 2 different
scopes, so it's not an error.

The analogous non-loop code would therefore be this, rather than what
you posted:

     int i = 0;
     {
          cout << i << endl;
          double i = 10.0;
          cout << i << endl;
     }

HTH,
~ Scott

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-09-04 13:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-04 13:34 scoping issue Jason_Haynberg
2008-09-04 13:41 ` Andrew Haley
2008-09-04 13:41 ` me22

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).