而Richard Smith在主题早期的部分earlier in the thread中指出了在C++11和C++14中在常量表达式中使用逗号运算符的一些用法:对于C++11,我建议允许使用逗号运算符,因为该限制似乎是武断的。我听到的所有禁止使用逗号运算符的理由都对我来说非常不可信和牵强。
请注意,有人认为他的C++11示例不正确,因为包含逗号运算符的表达式应该在I disagree with your argument and your conclusion. In C++11, the comma operator is useful within constexpr functions, because we aren't allowed multiple statements:
template<typename T> constexpr T my_array<T>::at(size_type n) { return (n < size() || throw "n too large"), (*this)[n]; }
In C++14, it's useful in essentially all of the cases where it's useful outside of constant expressions:
constexpr void do_stuff(int x[]) { for (int i = 0, j = 100; i != j; ++i, --j) x[i] = x[j]; }
More philosophically, we shouldn't ban things from constant expressions simply because we're not imaginative enough to find the cases where they're genuinely useful. Constant expressions should not be a semi-random sublanguage of C++, missing random features, to the extent that we can avoid that. These days, top-level commas are prohibited mostly because constant-expressions tend to occur in contexts where a comma would mean something else.
()
中,但他的示例给出了论点的要点。该论点基于第5.19
节常量表达式的语法。constant-expression:
conditional-expression
(expression)
,然后再从表达式到达逗号运算符。 T.C. 指出,由于相关部分在这一点上似乎含糊不清,因此可能并非如此。
()
(这是语法要求,需要一个条件表达式)。 - T.C.constexpr
函数的调用是一个“条件表达式”,因此符合语法,所以问题是当调用被“函数调用替换”时,是否产生一个常量表达式,而[dcl.constexpr]/p5中的规范对于“函数调用替换”所产生的确切表达式非常模糊。 - T.C.