This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: The scope of a for statement
- From: Joe Buck <Joe dot Buck at synopsys dot COM>
- To: Magnus Fromreide <magfr at lysator dot liu dot se>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Fri, 5 Mar 2010 12:06:01 -0800
- Subject: Re: The scope of a for statement
- References: <20100305193823.GA14322@stalhein.lysator.liu.se>
On Fri, Mar 05, 2010 at 11:38:23AM -0800, Magnus Fromreide wrote:
> Hello.
>
> I tried to do
>
> for (;; ({ break; }))
> printf("Hello\n");
>
> and got an error message:
>
> error: break statement not within loop or switch
But it only got through the parser, so that this error message
could be generated, because you're using a GNU extension: statements
and declarations in expressions. That is, ({ break;}) is a GNU
extension.
> when compiling it as C. Given that 9899:1999 §6.8.6.3 says that a break
> statement only shall appear in or as a switch or loop body that is expected.
>
> The problem is that when I compile it as C++ i get the same error message and
> 14882:1998 (and n3035) §6.5.3 says that
>
> The for statement
> for ( for-init-statement conditionopt ; expressionopt ) statement
> is equivalent to
> {
> for-init-statement
> while ( condition ) {
> statement
> expression ;
> }
> }
>
> and then goes on to list some exceptions to this, none of which are of
> importance here.
But in standard ISO C++, ({ break;}) is not a valid expression.
Ideally a GNU extension should be specified as well as the rest of the
standard is specified, but I'm not surprised that this doesn't work.