为什么逗号运算符的限制在C ++ 11中的常量表达式中删除? [英] Why was the restriction on the comma operator being in a constant expression removed in C++11?

查看:147
本文介绍了为什么逗号运算符的限制在C ++ 11中的常量表达式中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近当回答问题时,我发现在常数中允许使用逗号运算符 C ++ 11 中的表达式只要表达式被()包围,例如:

  int a [(1,2)]; 

Pre C ++ 11 禁止在常量表达式中使用逗号运算符 pre c ++ 11 standard section 5.19 常量表达式强调我):



>
增加,减少,函数调用或逗号运算符不得使用



$ b b

为什么在C ++ 11以前的常量表达式中不允许使用逗号运算符?为什么要解除此限制?

解决方案

p>我们可以从 std-讨论群组在常量表达式线程中的逗号运算符,其中Gabriel Dos Reis说:


对于C ++ 11我提议允许它,因为限制出现
任意和所有理由我听说为理由禁止出现
非常不令人信服和对我怀疑。




和Richard Smith 在线程中的早期注释了在C ++ 11和C ++ 14中的常量表达式中的逗号运算符


我不同意你的论点和你的结论。在C ++ 11中,逗号
运算符在constexpr函数中很有用,因为我们不是
允许多个语句:

  template< typename T> constexpr T my_array< T> :: at(size_type n){
return(n< size()|| thrown too large),(* this)[n] }

在C ++ 14中,它基本上适用于所有的情况,
在常量表达式外有用:

  constexpr void do_stuff(int x []){
for(int i = 0 ,j = 100; i!= j; ++ i,-j)
x [i] = x [j] }更多哲学上,我们不应该禁止常量
表达式中的事情,因为我们可以使用



<没有想象力,找到
的情况下,他们是真正有用的。常量表达式不应该是
是C ++的半随机子语言,缺少随机特征,到
范围,我们可以避免。现在,顶级逗号是
被禁止的,主要是因为常量表达式倾向于在
上下文中出现,其中逗号意味着别的东西。


$ b $注意,有人认为他的C ++ 11示例不正确,因为包含逗号运算符的表达式应该在()但是他的例子给出了参数的本质。参数将基于 5.19 部分的语法:

  constant-expression:
条件表达式



不是从条件表达式获得逗号运算符,但我们可以转到 primary-expression ,这会让我们进入表达式),然后我们可以从表达式获取逗号运算符



T.C。 指出可能不是这样,因为相关部分在这一点上似乎含糊不清。


Recently when answering a question I realized that the comma operator is allowed in a constant expression in C++11 as long as the expression is surrounded by (), for example:

int a[ (1, 2) ] ;

Pre C++11 it is forbidden to use the comma operator in a constant expression, from the draft pre C++11 standard section 5.19 Constant expressions which says (emphasis mine):

[...]In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used.

Why was the comma operator not allowed in a constant expression pre C++11 and why was this restriction lifted?

解决方案

We can find the answer to this in the std-discussion group Comma operator in constant-expression thread where Gabriel Dos Reis says:

For C++11 I proposed to allow it because the restriction appeared arbitrary and all reasons I've heard as rationale for its ban appear very unconvincing and specious to me.

and Richard Smith earlier in the thread notes some of the uses of the comma operator in a constant expression in both C++11 and C++14:

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.

Note it was argued that his C++11 example is not correct since the the expression containing the comma operator should be in () but his example gives the essence of the argument. The argument would be based on the grammar from section 5.19 Constant expressions:

constant-expression:
   conditional-expression

we can not get to comma operator from a conditional-expression but we can get to primary-expression which gets us to ( expression ) and we can then get to the comma operator from expression.

T.C. points out that this may not be the case since the relevant section seems vague on this point.

这篇关于为什么逗号运算符的限制在C ++ 11中的常量表达式中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆