c++cclgcc-extensions

Are code blocks inside parenthesis in C/C++ legal and can MSCL compile it?


I have the following code:

int x;
x = ({ 1; 2; 3; });
printf("%d\n", x); // should be 3

(If you're curious why I would ever write disgusting code like that. The answer is I'm not. I'm writing a generator that outputs C code, and having such a statement will make things a lot easier.)

The code compiles and works on Apple LLVM version 7.0.2 (with warnings for unused code of course) but fails with MSCL 10.0 and 14.0 (error C2059: syntax error: '{').

My question is: 1) is there a name for this kind of code(-abuse)? 2) Is it legal in any C/C++ standard? 3) Is there a way to get MSCL to accept it?


Solution

  • Don't know anything about MSCL part of the question since i've always used GCC. And in GCC:

    1) this is called compound statement expression;

    2) this is a non-standard GCC extension.