2

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?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Yifan
  • 4,867
  • 5
  • 25
  • 24
  • 7
    This could be a GCC [statement expression](https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html), which is a non-standard vendor extension. – Kerrek SB Dec 26 '15 at 22:55
  • I see. That makes sense. If there's no equivalent functionality in mscl, I guess I'll have to rework my generator. – Yifan Dec 26 '15 at 22:58
  • 1
    What is the point of `x = 1; 2; 3; ` even with `{}` or `({})` brackets? – Weather Vane Dec 26 '15 at 23:00
  • 3
    If you're in C++11, you could make it a lambda and immediately call it. `x = [](){ 1; 2; return 3; }();`. (I might have the syntax wrong; it's been awhile, but you get the idea) – Silvio Mayolo Dec 26 '15 at 23:01
  • @WeatherVane, I used that as a simple example. What I actually want is to run a bunch of arbitrary C statements and "return" a value. I was hoping I didn't have to go C++, but I guess I can do it with lambda functions. – Yifan Dec 26 '15 at 23:05
  • 2
    So it's an X-Y question? – Weather Vane Dec 26 '15 at 23:10
  • No, I asked the question I wanted to ask. Namely: can MSCL compile this kind of statement. Since it can't, I have to rework my code (turns out I can't use lambda functions since I need flow control inside the statements). – Yifan Dec 26 '15 at 23:15
  • You could generate `int x; { statement; statement; x = 3; }` – M.M Dec 26 '15 at 23:33
  • Closely related to [Are compound statements (blocks) surrounded by parens expressions in ANSI C?](http://stackoverflow.com/q/1238016/1708801) – Shafik Yaghmour Dec 27 '15 at 01:04

2 Answers2

8

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.

Oleg Andriyanov
  • 5,069
  • 1
  • 22
  • 36
  • Thanks, a quick google shows that cl doesn't have a similar feature. I guess I can use lambda functions instead. Didn't want to go C++ but I guess I have to. – Yifan Dec 26 '15 at 23:02
1

Instead of a compound statement expression, why not use the comma operator and just write:

x = ( 1, 2, 3 );
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I used that as an example, but what I actually need is something like ```({ statement; statement; 0 })``` where scoped variables might be defined. – Yifan Dec 26 '15 at 23:02
  • And why not write `x = 3;`? – juanchopanza Dec 26 '15 at 23:05
  • @Yifan: are you generating the code with the preprocessor or with your own code generator? – chqrlie Dec 26 '15 at 23:06
  • To be specific, I'm modifying CGEN and the hope is that I don't have to modify more than I have to. – Yifan Dec 26 '15 at 23:16
  • I'm afraid there is no solution for MSCL if `statement`s are not expressions. But if the value to store in `x` is constant and `x` is not used in the statements, you could of course generate: `x = 0; statement; statement;` – chqrlie Dec 26 '15 at 23:21
  • So right now, the code generates something like: ```x = READ_MEM(READ_REG(x))``` where I basically want to exit out when I see ```READ_REG```. I can do it with one line of modification if I'm allowed to change READ_REG to ```({ if (some_cond_on_x) break; 0; })``` otherwise, I'll have to do more work. – Yifan Dec 26 '15 at 23:25