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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It assigns user input value to
aand prints it out. it is done by using aStatement 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:
Always compile your code by selecting a standard in GCC: use one of the options
-ansi,-std=c90or-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-errorsif you want them to be errors rather than warnings).