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

4

83

I came across this weird C++ program.

#include <iostream>
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?

Alkylation answered 10/6, 2011 at 10:58 Comment(7)
@VJo: I mean, what is this "({})" construct called? Is it similar to anonymous functions?Alkylation
+1 Blimey, I had never seen this before. Too bad it's GNU C/C++ specific...Crutchfield
+1, looks little similar like lambda function :)Endothelium
Is it just me, or are there more bizarro non-standard extensions in gcc than in any other compiler?Nerland
@David Yeah, it is chock-full of the b*ggers. That's what makes using -pedantic almost essential.Boeotia
Removed the extraneous meta tag.Penza
It is a non-portable way of writing: int main() { int a; cin >> a; cout << a; }. I assume this must be a minimized example; there is no point in using the mechanism in this context, though there might be uses in other, larger contexts.Distance
H
74

It assigns user input value to a and prints it out. it is done by using a Statement Expression.

Statement Expressions are a gnu gcc compiler extension and are not supported by the C/C++ standards. Hence, any code which uses statement expression is not standard conforming and non-portable.

The IBM XL C/C++ v7.0 also support Statement Expressions & its documentation explains them aptly:

Statement Expressions:

A compound statement is a sequence of statements enclosed by braces. In GNU C, a compound statement inside parentheses may appear as an expression in what is called a Statement expression.

         .--------------.
         V              |
>>-(--{----statement--;-+--}--)--------------------------------><

The value of a statement expression is the value of the last simple expression to appear in the entire construct. If the last statement is not an expression, then the construct is of type void and has no value.

Always compile your code by selecting a standard in GCC: use one of the options -ansi, -std=c90 or -std=iso9899:1990, -std=c++03, -std=c++0x; to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).

Headcloth answered 10/6, 2011 at 11:2 Comment(0)
B
58

It's a GCC extension. Compile your code with the -pedantic flag if you want to get rid of stuff like this (and you really do want to).

Boeotia answered 10/6, 2011 at 11:7 Comment(2)
I suspect it got so many votes because many people got relieved after learning that something they hadn't heard of or used isn't standard anyway.Kirghiz
It appears to be not just a GCC extension nowadays. At least the C++ 2013 draft standard does define expression statements open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdfCalpe
K
18

It creates an inline scope, declares x within it, reads it from the standard input and the whole statement finally evaluates to x, which is assigned to a.

The comma operator works similarly, although it doesn't require a separate scope. For example:

int x;
int a = (cin >> x, x);

would do the same. All the statements connected with commas will be executed sequentially, and the result of the whole expression will be set to the value of the rightmost operand.

Kirghiz answered 10/6, 2011 at 11:1 Comment(4)
Is there any specific name for "({})"?Alkylation
@Koshimitsu: There is not, because it is a combination of operators (if you'd like to call them that). The curly brackets {} define the start ({) and end (}) of a scope in this case. The regular brackets are just there to encapsulate the scope's definition and return value. I am not entirely sure about whether these regular brackets can be omitted in this case, however I would expect they can.Oxblood
@Koshimitsu: I'm not very familiar with C++, but in C it's perfectly legal, and I've used it many times.Kirghiz
@Neil: well, maybe it's because gcc is the only C compiler I use, hence I got mislead.Kirghiz
N
13

I don't believe that this is standard C++. It's probably a compiler-specific extension that allows an inner scope to evaluate to a value.

Nauseating answered 10/6, 2011 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.