Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

the behavior of a break in an __extension__ for g++ is not the same for gcc version 12.2.0 and gcc version 5.4.0

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 or continue statement inside of a statement expression used in while, do or for loop or switch statement condition or for 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 or switch 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading