【发布时间】:2018-08-01 15:06:38
【问题描述】:
在C++ 中,可以执行以下操作:
int f(int& a, int& b, int& c, int& d) {
return (a, b, c, d); // here
}
首先,这个“括号”表达式的语言名称(以及 cppreference 上的链接)是什么,其中仅返回最后一项;
其次,假设代码变成:
int f(int& a, int& b, int& c, int& d) {
return (a += 2, d += 5, b -= 3, c = a + b + d, d); // here
}
这些“括号”表达式的求值顺序是固定的吗?如果是这样,我保证f 会从左到右更改a、b、c 和d 的值,并返回d 的正确更新值,就好像它是:
int f(int& a, int& b, int& c, int& d) {
a += 2;
d += 5;
b -= 3;
c = a + b + d;
return d;
}
最后,纯粹在C++11中(由于C++14中不再存在问题,因为constexpr没有那么强的要求),这个括号表达式是否可以用于在单个@中编写多个计算987654336@函数?
【问题讨论】:
-
IIUC 您正在寻找comma operator?,因为您询问了 constexpr,请参阅Why was the restriction on the comma operator being in a constant expression removed in C++11?
标签: c++ c++11 constexpr operator-precedence comma-operator