Issue
The context (bypass it if you don't care) :
I am compiling qt 4.8.7 on my Raspberry Pi 5 64b under Debian GNU/Linux 12 (bookworm) and I have problems including about the translation to produce the qm file from the ts file
I discover the problem concerns the Qt foreach which stops after the first element, and that because of a break in an __extension __ having (for me) a strange behavior.
The problem :
with that code reproducing the problem
#include <iostream>
int main()
{
for (int i = 0; i != 5; ++i)
for (int j = i;; __extension__ ({break;}))
std::cout << j << std::endl;
}
On my old ubuntu 16.04 with gcc version 5.4.0 20160609, the execution prints the number 0 up to 4.
But on my raspberry pi5 with gcc version 12.2.0 (Debian 12.2.0-14) the execution only writes 0, and if I replace the break by a continue the execution writes 0 up to 4. The break/continue concerns the outer loop, and this is visible if I try to compile :
#include <iostream>
int main()
{
for (int j = 1;; __extension__ ({break;}))
std::cout << j << std::endl;
}
producing error: break statement not within loop or switch
Is the behavior of __extension __ changed or is that a (known ?) bug ? Thank you
Solution
From https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html:
A
break
orcontinue
statement inside of a statement expression used inwhile
,do
orfor
loop orswitch
statement condition orfor
statement init or increment expressions jumps to an outer loop or switch statement if any (otherwise it is an error), rather than to the loop orswitch
statement in whose condition or init or increment expression it appears.
So the behaviour you see on 12.2.0 (only printing 0
, the break
breaking the outer loop) is the intended behaviour.
There was a bug before GCC 9 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44715) where in C++ mode, the break
would apply to the inner loop and in C mode, the break
would apply to the outer loop. So if you wish to support GCC 5.4.0, you cannot use break
in statement expressions.
And anyways, this is confusing to read. You would rather rewrite your code so your break
can be in the body of the loop rather than in the increment expression.
Answered By - Artyer Answer Checked By - Clifford M. (WPSolving Volunteer)