 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gordon Marr Guest
|
Posted: Fri Jan 23, 2004 9:04 pm Post subject: Legal C++ for loop? |
|
|
This compiles under G++ 3.2.2, but I hadn't come across the
for loop syntax before, so I'm curious to know whether it's
actually legal or not?
#include <iostream>
#include <string>
using namespace std;
int main()
{
for({int i = 0; string dots("");} i < 10; ++i)
{
cout << "i = " << i << ": " << dots << endl;
dots += '.';
}
}
The output is:
i = 0:
i = 1: .
i = 2: ..
i = 3: ...
i = 4: ....
i = 5: .....
i = 6: ......
i = 7: .......
i = 8: ........
i = 9: .........
The given email is invalid; please follow up to the group.
Thank you!
GM.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Jan 24, 2004 1:22 pm Post subject: Re: Legal C++ for loop? |
|
|
"Gordon Marr" <bitbucket (AT) www (DOT) pointless.com> wrote
| Quote: | This compiles under G++ 3.2.2, but I hadn't come across the
for loop syntax before, so I'm curious to know whether it's
actually legal or not?
Not legal. Some goofy figment of the GCC. |
The initial stuff in a for loop has to be either an expression-statement
or a simple declaration. Compound statements are right out.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Erik Max Francis Guest
|
Posted: Sat Jan 24, 2004 9:36 pm Post subject: Re: Legal C++ for loop? |
|
|
Gordon Marr wrote:
| Quote: |
This compiles under G++ 3.2.2, but I hadn't come across the
for loop syntax before, so I'm curious to know whether it's
actually legal or not?
|
No, it is a gcc extension. gcc does have an issue with creating their
own extensions willy nilly, but at least it is pretty good about
reporting them when in -ansi -pedantic mode:
max@oxygen:~/tmp% g++ -W -Wall -ansi -pedantic loop.cc -o loop
loop.cc: In function `int main()':
loop.cc:8: warning: ISO C++ forbids compound statements inside for
initializations
--
__ Erik Max Francis && [email]max (AT) alcyone (DOT) com[/email] && http://www.alcyone.com/max/
/ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
__/ I go out with actresses because I'm not apt to marry one.
-- Henry Kissinger
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Radu Grigore Guest
|
Posted: Sat Jan 24, 2004 9:37 pm Post subject: Re: Legal C++ for loop? |
|
|
Gordon Marr <bitbucket (AT) www (DOT) pointless.com> wrote
| Quote: | This compiles under G++ 3.2.2, but I hadn't come across the
for loop syntax before, so I'm curious to know whether it's
actually legal or not?
for({int i = 0; string dots("");} i < 10; ++i)
|
It does not compile on VC7.1 and I strongly suspect it's a problem
with gcc's grammar. But I can't be sure since I don't have a standard
handy now.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bert Klaps Guest
|
Posted: Sat Jan 24, 2004 9:44 pm Post subject: Re: Legal C++ for loop? |
|
|
Gordon Marr <bitbucket (AT) www (DOT) pointless.com> wrote
| Quote: | This compiles under G++ 3.2.2, but I hadn't come across the
for loop syntax before, so I'm curious to know whether it's
actually legal or not?
#include
#include
using namespace std;
int main()
{
for({int i = 0; string dots("");} i < 10; ++i)
{
cout << "i = " << i << ": " << dots << endl;
dots += '.';
}
}
|
It's not legal: it's got a compound statement where
an expression or declaration is expected.
By the way, under G++ 3.3.1 this compiles too.
E.g. Comeau online correctly says:
line 8: error: expected an expression
for({int i = 0; string dots("");} i < 10; ++i)
^
Regards,
Bert Klaps
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sebastian Kapfer Guest
|
Posted: Sat Jan 24, 2004 9:45 pm Post subject: Re: Legal C++ for loop? |
|
|
On Fri, 23 Jan 2004 16:04:06 -0500, Gordon Marr wrote:
| Quote: | This compiles under G++ 3.2.2, but I hadn't come across the
for loop syntax before, so I'm curious to know whether it's
actually legal or not?
#include <iostream
#include
using namespace std;
int main()
{
for({int i = 0; string dots("");} i < 10; ++i)
{
cout << "i = " << i << ": " << dots << endl;
dots += '.';
}
}
|
g++ -pedantic -Wall -o test2 test2.cpp
test2.cpp: In function `int main()':
test2.cpp:9: error: ISO C++ forbids compound statements inside for
initializations
So I think the g++ programmers themselves think that it is an extension.
(I think it's strange scoping in addition. Why would i and dots be visible
in the loop body at all? They're declared in their own block.)
--
Best Regards, | This signature is currently under construction.
Sebastian | Please check back later!
| Quote: | --------------------------------------------------------
mailbox in "From" silently drops any mail > 20k
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Robert Kindred Guest
|
Posted: Mon Jan 26, 2004 8:30 pm Post subject: Re: Legal C++ for loop? |
|
|
"Gordon Marr" <bitbucket (AT) www (DOT) pointless.com> wrote
| Quote: | This compiles under G++ 3.2.2, but I hadn't come across the
for loop syntax before, so I'm curious to know whether it's
actually legal or not?
#include
#include
using namespace std;
int main()
{
for({int i = 0; string dots("");} i < 10; ++i)
|
I believe I have seen commas used here, such as:
for(int i = 0, string dots(""); i < 10; ++i)
| Quote: | {
cout << "i = " << i << ": " << dots << endl;
dots += '.';
}
}
snip
Thank you!
GM.
|
hth, Robert Kindred
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Tue Jan 27, 2004 10:45 am Post subject: Re: Legal C++ for loop? |
|
|
Gordon Marr wrote:
| Quote: | This compiles under G++ 3.2.2, but I hadn't come across the
for loop syntax before, so I'm curious to know whether it's
actually legal or not?
for({int i = 0; string dots("");} i < 10; ++i)
{...}
|
for( pair
{...}
Just in case you didn't only wonder if it's legal but also if it's
necessary.
cheers
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Tue Jan 27, 2004 11:07 am Post subject: Re: Legal C++ for loop? |
|
|
| Quote: | Gordon Marr <bitbucket (AT) www (DOT) pointless.com> wrote
for( {int i = 0; string dots("");} i < 10; ++i)
{ ... }
|
[email]bertklaps (AT) yahoo (DOT) com[/email] (Bert Klaps) wrote
| Quote: | It's not legal: it's got a compound statement where
an expression or declaration is expected.
|
Wouldn't it be great if this was legal?
Currently you can't declare two variables in a for() loop, unless they
are the same type. Wouldn't it be great if the example above worked?
The legal version uses a local block around the for:
{ string dots(""); for (int i=0; i<10; ++i)
{ ... }
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gordon Marr Guest
|
Posted: Tue Jan 27, 2004 3:02 pm Post subject: Re: Legal C++ for loop? |
|
|
Robert Kindred wrote:
| Quote: | "Gordon Marr" <bitbucket (AT) www (DOT) pointless.com> wrote in message
for({int i = 0; string dots("");} i < 10; ++i)
I believe I have seen commas used here, such as:
for(int i = 0, string dots(""); i < 10; ++i)
{
cout << "i = " << i << ": " << dots << endl;
dots += '.';
}
|
I tried that when I first saw this, and got:
loop.C: In function `int main()':
loop.C:8: error: parse error before `(' token
loop.C:10: error: `dots' undeclared (first use this function)
[the for(...) line is line 8]
Basically, the compiler (G++ 3.3.2) thinks I'm trying to declare
more integers. This works:
for(int i = 0, dots = 0; i < 10; ++i)
but obviously, no strings.
GM.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Jan 27, 2004 6:43 pm Post subject: Re: Legal C++ for loop? |
|
|
In message <7f2735a5.0401261813.73151212 (AT) posting (DOT) google.com>, Allan W
<allan_w (AT) my-dejanews (DOT) com> writes
| Quote: | Gordon Marr <bitbucket (AT) www (DOT) pointless.com> wrote
for( {int i = 0; string dots("");} i < 10; ++i)
{ ... }
[email]bertklaps (AT) yahoo (DOT) com[/email] (Bert Klaps) wrote
It's not legal: it's got a compound statement where
an expression or declaration is expected.
Wouldn't it be great if this was legal?
|
Why? Objects declared within a block are destroyed at the end of a block
so we would have to mess with far more of the language and introduce
inconsistencies if we were to make that syntax useful.
| Quote: |
Currently you can't declare two variables in a for() loop, unless they
are the same type. Wouldn't it be great if the example above worked?
|
Nonsense, we just declare them as a pair
| Quote: |
The legal version uses a local block around the for:
{ string dots(""); for (int i=0; i<10; ++i)
{ ... }
}
|
Well that is one but
for(pair
{...}
is fine. For more than two you need a Boost tuple (I think).
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Tue Jan 27, 2004 6:48 pm Post subject: Re: Legal C++ for loop? |
|
|
Hi,
| Quote: | for( {int i = 0; string dots("");} i < 10; ++i)
{ ... }
It's not legal: it's got a compound statement where
an expression or declaration is expected.
Wouldn't it be great if this was legal?
|
No, I don't think so. It breaks with the rules of the language. By
them, the lifetime of the variable "i" ends at the curly bracket in
the head of the for(..) loop, thus the "i" in the body would/should
not exist, or would refer to another global "i". Consider the following:
foo(..)
{
int i,l;
for( {int i = 0;short k = 1;l = 0 }; i < 10; ++i) {
l += i + k; // which "i" is this?
}
}
| Quote: | Currently you can't declare two variables in a for() loop, unless they
are the same type. Wouldn't it be great if the example above worked?
The legal version uses a local block around the for:
{ string dots(""); for (int i=0; i<10; ++i)
{ ... }
}
|
Unless you insist on destroying "dots" immediately after the for() loop
terminated, there's no need for the block at all.
Sorry, I fail to see why this is an improvement. It messes with language
rules and makes the code harder to read.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Jan 28, 2004 8:28 pm Post subject: Re: Legal C++ for loop? |
|
|
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | In message <7f2735a5.0401261813.73151212 (AT) posting (DOT) google.com>, Allan W
[email]allan_w (AT) my-dejanews (DOT) com[/email]> writes
Gordon Marr <bitbucket (AT) www (DOT) pointless.com> wrote
for( {int i = 0; string dots("");} i < 10; ++i)
{ ... }
[email]bertklaps (AT) yahoo (DOT) com[/email] (Bert Klaps) wrote
It's not legal: it's got a compound statement where
an expression or declaration is expected.
Wouldn't it be great if this was legal?
Why? Objects declared within a block are destroyed at the end of a
block so we would have to mess with far more of the language and
introduce inconsistencies if we were to make that syntax useful.
|
Well, I'm not sure that it's all that necessary either. But...
| Quote: | Currently you can't declare two variables in a for() loop, unless
they are the same type. Wouldn't it be great if the example above
worked?
Nonsense, we just declare them as a pair
|
Which is an excellent solution if the logical names for the two objects
are "first" and "second", and totally unacceptable otherwise.
| Quote: | The legal version uses a local block around the for:
{ string dots(""); for (int i=0; i<10; ++i)
{ ... }
}
Well that is one but
for(pair
{...}
is fine.
|
If you like obfuscation. I prefer using names that mean something, so
that others can read my code.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Balog Pal Guest
|
Posted: Thu Jan 29, 2004 9:48 am Post subject: Re: Legal C++ for loop? |
|
|
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | Currently you can't declare two variables in a for() loop, unless they
are the same type. Wouldn't it be great if the example above worked?
Nonsense, we just declare them as a pair<type1, type2>.
|
That reminds me another issue. Why is const discarded in multiple onject
declaration:
for(int i=0, const maxI = 10; i < maxI; ++i) ...
I get:
warning C4228: nonstandard extension used : qualifiers after comma in
declarator list are ignored
while a * shall be repeated, and is accepted, why not const?
Paul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gennaro Prota Guest
|
Posted: Thu Jan 29, 2004 7:13 pm Post subject: Re: Legal C++ for loop? |
|
|
On 29 Jan 2004 04:48:51 -0500, "Balog Pal" <pasa (AT) lib (DOT) hu> wrote:
| Quote: | "Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
news:XIO0kdCJokFAFwQE (AT) robinton (DOT) demon.co.uk...
Currently you can't declare two variables in a for() loop, unless they
are the same type. Wouldn't it be great if the example above worked?
Nonsense, we just declare them as a pair<type1, type2>.
That reminds me another issue. Why is const discarded in multiple onject
declaration:
for(int i=0, const maxI = 10; i < maxI; ++i) ...
|
It's not "discarded", it's an error. After the comma in a
simple-declaration you can only have *declarators*, and they cannot
start with a cv-qualifier (they may contain one, but not as the first
token. Example:
int i = 0, * const p = 0; // ok, the const in the second
// declarator is after the *
// (ptr-operator)
)
Genny.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|