Hello.

At the moment compilation of a range-based for in c++98 fails with the
error message:

foo.cpp: In function 'int foo()':
foo.cpp:4:13: error: expected initializer before ':' token
foo.cpp:6:1: error: expected primary-expression before '}' token
foo.cpp:6:1: error: expected ';' before '}' token
foo.cpp:6:1: error: expected primary-expression before '}' token
foo.cpp:6:1: error: expected ')' before '}' token
foo.cpp:6:1: error: expected primary-expression before '}' token
foo.cpp:6:1: error: expected ';' before '}' token

This is quite unreadable and not very informative.

In order to fix this I applied a patch that gives the output:

foo.cpp: In function 'int foo()':
foo.cpp:4:11: error: range-based for only available with -std=c++0x or 
-std=gnu++0x

but additionally it changes the output of g++.old-deja/g++.mike/for2.C
when compiled in c++98 mode.

Here there are two problems

#1 - It doesn't change the output of g++.old-deja/g++.mike/for2.C when
     compiled in c++0x-mode
#2 - It does change the output of g++.old-deja/g++.mike/for2.C when
     compiled in c++98-mode

#1 is a problem since for2.C only contains old-style for loops and so it
is ridiculous to say things like:

for2.C:4:16: error: types may not be defined in range-based for loops

and my patch extends that behavior to the c++98 case where it makes even
less sense...

Now, the good part is that solving problem #1 should solve problem #2 as
well.

(and yes, I know I shouldn't use error but I have yet to find the
location of the colon for error_at)

/MF
Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c	(revision 164381)
+++ gcc/cp/parser.c	(working copy)
@@ -8666,6 +8666,11 @@
   /* The next token should be `:'. */
   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
     cp_parser_simulate_error (parser);
+  else if (cxx_dialect == cxx98)
+    {
+      error ("range-based for only available with -std=c++0x or -std=gnu++0x");
+      return NULL_TREE;
+    }
 
   /* Check if it is a range-based for */
   if (!cp_parser_parse_definitely (parser))
@@ -8922,10 +8927,7 @@
 	/* Look for the `('.  */
 	cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
 
-	if (cxx_dialect == cxx0x)
-	  statement = cp_parser_range_for (parser);
-	else
-	  statement = NULL_TREE;
+	statement = cp_parser_range_for (parser);
 	if (statement == NULL_TREE)
 	  statement = cp_parser_c_for (parser);
 
int foo()
{
	int v[] = { 1 };
	for (int i : v)
		return i;
}

Reply via email to