我有以下代码:
int main() {
int i=0;
int j=({int k=3;++i;})+1; // this line
return 0;
}
它编译并运行。如果我从“这一行”中删除 ()
,则它不会编译。
我只是好奇这里应用了什么语法规则。
{}
包含2条语句,最后一条语句表示该代码块的“返回”值。那为什么它需要一个额外的 ()
对来使这个返回值可用呢?
最佳答案
那是 statement expression ,它是一个 GCC 特定的扩展。
来自链接引用:
A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.
复合语句是花括号括起来的语句 block 。
关于c++ - C++中 "()"如何将语句转换为表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52313218/