2

I countered the following code for the first time. I am confused about its syntax and why it would work.

int b = ({
        int c = 2;
        c;
    });
std::cout << b;
Elazar
  • 20,415
  • 4
  • 46
  • 67
Zack
  • 1,205
  • 2
  • 14
  • 38
  • 3
    It's a GNU/g++ extension and is known as a *statement expression*. Looking for a suitable duplicate ... – Adrian Mole Oct 02 '21 at 22:20
  • I'm wondering why does that pass clang++ without a warning. – Elazar Oct 02 '21 at 22:21
  • In any case: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html – Elazar Oct 02 '21 at 22:21
  • 1
    @Elazar Well, clang-cl gives me: *warning : use of GNU statement expression extension [-Wgnu-statement-expression]* – Adrian Mole Oct 02 '21 at 22:22
  • 1
    You can compile with `-pedantic` to get warnings about non-standard extension. In this case, you'd get the warning _"warning: use of GNU statement expression extension [-Wgnu-statement-expression]"_. – Brian61354270 Oct 02 '21 at 22:22

0 Answers0