Questions tagged [gcc-statement-expression]

GCC's C and C++ extension allowing the use of (usually compound) statements as expressions, such that the value of the last one is considered the expression value

GCC's C and C++ extension allowing expressions which are (blocks of) statements, such that the value of the last one is considered the expression value

In C and C++, a statement does not serve as an expression. One of GCC's supported extensions, however, is allowing statements to be interpreted as expressions - the usually-discarded value of the outermost operator, functional call etc. In the case of a compound statement - a curly-braces block - the expression value is that of the last statement in the block.

This allows doing work within the block such as defining variables and using them in the computation of a value

Statement expressions are described in the GCC documentation:

Statements and Declarations in Expressions

9 questions
83
votes
4 answers

What's this C++ syntax that puts a brace-surrounded block where an expression is expected?

I came across this weird C++ program. #include using namespace std; int main() { int a = ({int x; cin >> x; x;}); cout << a; } Can anyone explain what is going on? What is this construct called?
21
votes
2 answers

What is ({}) called in C++?

I just read code like this: auto value = ({ auto it = container.find(key); it != container.end() ? it->second : default_value; }); What is this ({}) called? I don't think I've ever seen this before.
Hesky Fisher
  • 1,145
  • 7
  • 14
4
votes
1 answer

Using comma operator (a, b) when a is a block of code

#include int main() { int sum = 0, result = (({for (int i = 0; i < 5; i++) sum += i;}), sum); printf("result = %d\n", result); return 0; } displays result = 10 I'm using whatever the default version of C is in gcc. Is…
NoComprende
  • 731
  • 4
  • 14
2
votes
0 answers

C++ curly brace inside parentheses

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;
Zack
  • 1,205
  • 2
  • 14
  • 38
2
votes
2 answers

What is this strange expression in GCC/Clang?

I have recently noticed an strange valid C/C++ expression in GCC/Clang which I have never seen before. Here is the example in C++, but similar expression works in C too: int main(){ int z = 5; auto x = ({z > 3 ? 3 : 2;}); // <-- expression …
Afshin
  • 8,839
  • 1
  • 18
  • 53
2
votes
1 answer

Execution order in GCC statement expressions

I found that there is a different execution order between two similar statements(the only difference is the below one has an additional ;). The destructor order is different. Does C++ have a corresponding specification about that or it's only an…
eddie kuo
  • 716
  • 1
  • 5
  • 13
1
vote
2 answers

Why `error: jump into statement expression` only sometimes?

I have two programs that use the same tricks and features, and only one of them compiles. A) This one compiles, and also works as expected: #include #include #include #include #include /* * int…
0
votes
1 answer

What rules of C++11 standard are used to determine the type of the expression in ({ ... })

I hasn't understand what compiler does here and why it's working c++ code #include int main() { printf( ({ // (1) struct some_noize_struct { // there may be another code }; …
0
votes
2 answers

GCC evaluation of compound statement

I found the following construct, where a variable is assigned to what seems to be a compound statement, foo, in a driver. For comparison, bar yields undefined behaviour be treating the same code as a proper function. It doesn't seem to conform with…
nwn
  • 583
  • 7
  • 23