0

I am developing cross-platform project, and I found something strange. in visual studio, the following code will fail. But g++/clang++ in Linux environment can compile without error. In addition, I search for keyword on VS, but found nothing. Anyone the reason?

int main(){
  ({;});
  return 0;
}
error C2059: syntax error: '{'
error C2143: syntax error: missing ';' before '{'
error C2059: syntax error: ')'

Visual Studio version I have : Microsoft Visual Studio Professional 2019, Version 16.5.1

CY.Lee
  • 1
  • 5

2 Answers2

0

In visual studio, it is interpreted as vc++. In c++, ; marks end of statement. So your code ({;}); is exactly taken by compiler as 1 ({; and 2 }); as a statement which is synaxtically not correct. Check syntax of wrting code in c++. I dont know about g++/clang. Sorry there.

user9559196
  • 157
  • 6
0

Compiling with clang++ using the -pedantic -Werror flags we see

dummy.cc:2:4: error: use of GNU statement expression extension [-Werror,-Wgnu-statement-expression]
  ({;});
   ^
1 error generated.

The MSVC compiler (cl, typically used by Visual Studio) does not support many non-standard extensions specific to GCC, including its statement expressions.

In this case, the block {;} is treated as a (useless) statement expression:

{
  /* nothing */
  ;
  /* nothing */
}

The outermost parentheses ((...)) are there to ensure the parser treats the block as an expression, rather than a more typical empty code block.

obataku
  • 29,212
  • 3
  • 44
  • 57