Ç逗号操作符 [英] C comma operator

查看:118
本文介绍了Ç逗号操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一个逗号操作符的内部规定(如下面的例子)除权pression不被认为是恒定的前pression?

Why is the expression specified inside a comma operator (such as the example below) not considered a constant expression?

例如,

int a = (10,20) ;

当在全球范围内给

产生一个错误初始化不是一个常数,虽然用逗号分开运营两个前pressions是常数(恒定的前pressions)。为什么整个前pression不被视为一个常量前pression?为了澄清我已阅读什么的,运营商不使用C ?采用C逗号操作符的。他们没有处理逗号运算符的这一方面。

when given in global scope yields an error "initializer is not a constant", though both the expressions separated by a comma operator are constants (constant expressions). Why is the entire expression is not treated as a constant expression? For clarification I have read What does the ‘,’ operator do in C? and Uses of C comma operator. They have not dealt this aspect of comma operator.

推荐答案

6.6 / 3 ,恒恩pressions时,ISO C99标准是您需要的部分。它指出:

Section 6.6/3, "Constant expressions", of the ISO C99 standard is the section you need. It states:

恒前pressions不得含有赋值,递增,递减函数调用,
  或逗号运营商,当它们包含在SUBEX pression不是内除
  评估。

Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.

在从ISO C99的理由的文件,有这个小片断:

In the C99 rationale document from ISO, there's this little snippet:

这是整型常量前pression必须包括只有数字在转换时可知,与运营商无副作用。

An integer constant expression must involve only numbers knowable at translation time, and operators with no side effects.

和,因为没有点使用逗号操作的所有的,如果你不依赖的副作用,它是在一个恒定的前pression没用的。

And, since there's no point in using the comma operator at all if you're not relying on side effects, it's useless in a constant expression.

到了,我的意思有两个code段之间绝对没有区别:

By that, I mean there's absolutely no difference between the two code segments:

while (10, 1) { ... }
while     (1) { ... }

因为 10 不实际的的东西。事实上,

since the 10 doesn't actually do anything. In fact,

10;

是一个完全有效的,虽然不是非常有用,C说法,一些大多数人不COM prehend直到搞懂的语言越好。

is a perfectly valid, though not very useful, C statement, something most people don't comprehend until they get to know the language better.

然而,是的这两个语句之间的差异:

However, there is a difference between these two statements:

while (  10, 1) { ... }
while (x=10, 1) { ... }

有在后者用逗号的是设定一个副作用变量 X 10

There's a side effect in the latter use of the comma operator which is to set the variable x to 10.

至于为什么他们不喜欢在不断的前pressions副作用,不断前pressions的整点是,它们可以在编译时进行评估,而无需执行环境 - ISO使翻译(编译时)和执行(运行时)的环境中。

As to why they don't like side effects in constant expressions, the whole point of constant expressions is that they can be evaluated at compile-time without requiring an execution environment - ISO makes a distinction between translation (compile-time) and execution (run-time) environments.

的线索,为什么ISO决定不要求编译器提供执行环境信息(不包含在头文件中其他的东西,如 limits.h中)可发现一个小后来理文档:

The clue as to why ISO decided against requiring compilers to provide execution environment information (other than stuff contained in header files such as limits.h) can be found a little later in the rationale document:

不过,虽然实现是肯定不允许产生翻译和执行环境完全相同的结果,要求这被认为是对许多交叉编译器无法容忍的负担。

However, while implementations are certainly permitted to produce exactly the same result in translation and execution environments, requiring this was deemed to be an intolerable burden on many cross-compilers.

在换句话说,ISO不想交叉编译器的制造商能够携带的执行环境为每一个可能的目标所累

In other words, ISO didn't want the manufacturers of cross-compilers to be burdened with carrying an execution environment for every possible target.

这篇关于Ç逗号操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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