This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: The scope of a for statement


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]