Range-based for in c++98
Magnus Fromreide
magfr@lysator.liu.se
Sun Sep 19 09:13:00 GMT 2010
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: range-based-for-diagnosis.diff
Type: text/x-patch
Size: 910 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20100919/8956cbfb/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: foo.cpp
Type: text/x-c++src
Size: 61 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20100919/8956cbfb/attachment-0001.bin>
More information about the Gcc
mailing list