在恒定初始器中使用逗号操作员的明显GCC错误
我在CCC中弄乱了CCC中的一个明显的错误:
#include <stdio.h>
int y = (1, 2);
int main()
{
return 0;
}
在GCC中给出错误:初始化器元素在Clang中工作正常时不是常数
。正如预期的那样,将声明放入main()
函数中也可以与两个编译器一起使用。
括号是必要的,因为除其他地方,而在不称赞逗号操作员方面是正确的。
尽管GCC代码片段看起来像这样:
3 | int y = (1, 2);
| ^
括号并不是问题。 int y =(1 + 2);
正常工作。
我知道逗号运算符在不变的表达中永远不需要,因为永远不应该有任何副作用,但是GCC将其视为错误仍然很奇怪,而不仅仅是打印警告:表达结果未使用
((这也是如此)。
这是一个错误吗?
I was messing around with C and discovered an apparent bug in GCC:
#include <stdio.h>
int y = (1, 2);
int main()
{
return 0;
}
in gcc gives error: initializer element is not constant
while it works fine in clang. And as expected, putting the declaration inside the main()
function also works with both compilers.
The parentheses are necessary, as otherwise gcc would be correct in disallowing the comma operator.
Despite the gcc code snippet looking like this:
3 | int y = (1, 2);
| ^
the parentheses are not the problem. int y = (1 + 2);
works fine.
I know that the comma operator is never necessary in constant expressions, as there should never be any side effects, but it's still odd that GCC sees it as an error instead of just printing warning: expression result unused
(which it also does).
Is this a bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据标准,逗号操作员为不允许以恒定表达方式 ,但Clang在大多数(但不是全部)上下文中承认这一点。例如:
live
GCC拒绝所有这些结构。
该标准确实允许实现接受其他形式的恒定表达式不过,但是尚不清楚是否应该记录这些其他形式,或者应该如何在不同的情况下接收它们。
有一个 clang bug 关于clang有时会以恒定表达方式允许的clange clang。
Per the standard, comma operator is not allowed in constant expressions, but clang admits it in most (but not all) contexts. For example:
Live
gcc rejects all of these constructs.
The standard does allow an implementation to accept other forms of constant expressions though, but it isn't clear if these other forms should be documented or how they should be admitted in different contexts.
There is a clang bug about clang sometimes treating comma as allowed in constant expressions.